tags: ACM
link:
Source: Cattle passenger network
Sudoku Subrectangles
Time limit: C / C ++ 1 second, 2 seconds other languages
Space limitations: C / C ++ 32768K, 65536K other languages
Special Judge, 64bit IO Format: %lld
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.
Example 1
copy
2 3 AaA caa
copy
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).
Example 2
copy
4 5 abcde fGhij klmno pqrst
copy
150
For sample 2, the grid has 150 nonempty subrectangles, and all of them are sudoku-like.
Question is intended: to give you a string of n * m matrix (containing only lowercase letters), if one of these sub-rectangles each row, each column of letters are not the same, called the rectangle
“sudoku-like. "How many seeking such a sub-rectangular.
Ideas: the game when reading the wrong questions, and that all the elements are not the same sub-rectangle. . . Really only need one per line each column can not have the same character. . . This is the road that simulated violence ah. . . Time complexity of n * m * 52. . . Constant write bigger does not matter. . Data is relatively weak. . . I said how much the number of flies. . . Violence seeking to reach every point furthest to the right and below the position, then the upper left corner point of each rectangular enumerated vertex, then the vertical length of the rectangular sequentially enumerated two sides and then find the remaining length of the two sides, the accumulated The answer can be.
Code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1010;
int m,n,k;
char s[maxn][maxn];
int id,r[maxn][maxn],d[maxn][maxn];
int b[maxn];
int stk[maxn];
ll ans;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(b,0,sizeof(b));
memset(d,0,sizeof(d));
memset(r,0,sizeof(r));
id=0;ans=0;
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
id++;
for(int k=j;k<=m;k++)
{
if(b[s[i][k]]==id)
{
r[i][j]=k-1;
break;
}
b[s[i][k]]=id;
}
if(!r[i][j]) r[i][j]=m;
id++;
for(int k=i;k<=n;k++)
{
if(b[s[k][j]]==id)
{
d[i][j]=k-1;
break;
}
b[s[k][j]]=id;
}
if(!d[i][j]) d[i][j]=n;
}
memset(stk,0,sizeof(stk));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
for(int k=i;k<=d[i][j];k++)
{
stk[k]=r[k][j];
if(k>i&&stk[k]>stk[k-1])
stk[k]=stk[k-1];
}
int top=d[i][j];//???
for(int k=j;k<=r[i][j];k++)
{
if(top>d[i][k]) top=d[i][k];
while(top>=i&&stk[top]<k)top--;
ans+=(ll)(top-i+1);
//cout<<top<<" "<<i<<endl;
}
}
printf("%lld\n",ans);
}
return 0;
}
link: Source: Cattle passenger network Rikka with Nickname Time limit: C / C ++ 2 seconds and 4 seconds in other languages Space limitations: C / C ++ 262144K, other languages 524288K 64bit I...
Title Description Chiaki has just learned hash in today's lesson. A hash function is any function that can be used to map data of arbitrary size to data of fixed size. As a beginner, Chiaki simply cho...
link: Source: Cattle passenger network 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 poss...
Meaning of the questions: Given a character matrix contains only uppercase and lowercase letters, asked how many sub-matrix is the number of characters alone (any row or column is not repeate...
Niu Ke.com Summer ACM Multi-School Training Camp (Game 7)-J-Sudoku Subrectangles Topic: Knowing a matrix with n*m full of characters, find out how many sub-matrices, each row and column do not have th...
link: Source: Cattle passenger network 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 poss...
link: Source: Cattle passenger network Title Description Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2...
Problem solution: CDQ can maintain the complexity of nlogn ...
So the answer is nothing more than a few, all with either 2 or 3 are used, 2 or 3 main supplement, 2 or 3 main fill....
There are n students going to travel. And hotel has two types room:double room and triple room. The price of a double room is p2 and the price of a triple room is p3 Now you need to calulate the minim...