链接:https://www.nowcoder.com/acm/contest/145/J
来源:牛客网
题目描述
You have a n * m grid of characters, where each character is an English letter (lowercase or uppercase, which means there are a total of 52 different possible letters).
A nonempty subrectangle of the grid is called sudoku-like if for any row or column in the subrectangle, all the cells in it have distinct characters.
How many sudoku-like subrectangles of the grid are there?
输入描述:
The first line of input contains two space-separated integers n, m (1 ≤ n, m ≤ 1000). The next n lines contain m characters each, denoting the characters of the grid. Each character is an English letter (which can be either uppercase or lowercase).
输出描述:
Output a single integer, the number of sudoku-like subrectangles.
示例1
输入
复制
2 3 AaA caa
输出
复制
11
说明
For simplicity, denote the j-th character on the i-th row as (i, j). For sample 1, there are 11 sudoku-like subrectangles. Denote a subrectangle by (x1, y1, x2, y2), where (x1, y1) and (x2, y2) are the upper-left and lower-right coordinates of the subrectangle. The sudoku-like subrectangles are (1, 1, 1, 1), (1, 2, 1, 2), (1, 3, 1, 3), (2, 1, 2, 1), (2, 2, 2, 2), (2, 3, 2, 3), (1, 1, 1, 2), (1, 2, 1, 3), (2, 1, 2, 2), (1, 1, 2, 1), (1, 3, 2, 3).
示例2
输入
复制
4 5 abcde fGhij klmno pqrst
输出
复制
150
说明
For sample 2, the grid has 150 nonempty subrectangles, and all of them are sudoku-like.
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int maxn=1005;
/*
题目大意:意思很明显,
给定一个方形矩阵只包含52个字符,
求子矩阵的数量,要求每行每列数字都不一样。(不是所有的数字)
数据量诱人,
但我觉得比第三题要难,,,(这题的搜索里面参杂了很多技巧)。
首先可以预处理出所有位置字符行中下一个出现的位置,和列中下一个出现的位置,
等等!能不能再优化下?该字符的界限可以和下一个字符的界限合并取最小。
下面就是参杂着计数原理的技巧了(好像也谈不上,但是找不到名词了)
对于每行,我们都可以对答案进行计数,只要找到最靠右的边界即可。
但有陷阱,因为枚举到的该行可能再枚举过程中和上一行的某个下界冲突,
但仔细想想其一定是递减的,即往下扩展的下界,一定是递减的(可参考代码研究),
这下就可以(有点像单调栈),利用枚举到的右边界进行回退即可,
最终按每行计数。
*/
int n,m;
char s[maxn];
int mp[maxn][maxn];
int pre[maxn][maxn];///行,下一个出现的位置
int col[maxn][maxn];///列,下一个出现的位置
bitset<100> bt[maxn];///无用
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=0;j<m;j++)
{
if(s[j]>='a'&&s[j]<='z') mp[i][j]=s[j]-'a';
if(s[j]>='A'&&s[j]<='Z') mp[i][j]=s[j]-'A'+26;
}
}
int show[200];
for(int i=0;i<n;i++)
{
for(int j=0;j<127;j++) show[j]=m;
pre[i][m]=m;
for(int j=m-1;j>=0;j--)
{
pre[i][j]=pre[i][j+1];
pre[i][j]=min(pre[i][j],show[mp[i][j]]);
show[mp[i][j]]=j;
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<127;j++) show[j]=n;
col[n][i]=n;
for(int j=n-1;j>=0;j--)
{
col[j][i]=col[j+1][i];
col[j][i]=min(col[j][i],show[mp[j][i]]);
show[mp[j][i]]=j;
}
}
int pos[maxn];
ll ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int g=col[i][j];
for(int c=j;c<pre[i][j];c++)
{
g=min(g,col[i][c]);
pos[c]=g;
}
g=pre[i][j]-1;
for(int c=i;c<col[i][j];c++)
{
g=min(g,pre[c][j]-1);
while(pos[g]<=c) g--;
ans+=g-j+1;
}
}
printf("%lld\n",ans);
return 0;
}