Largest Submatrix
http://acm.hdu.edu.cn/showproblem.php?pid=2870
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Now here is a matrix with letter
'a','b','c','w','x','y','z' and you can change 'w' to 'a' or 'b', change 'x' to
'b' or 'c', change 'y' to 'a' or 'c', and change 'z' to 'a', 'b' or 'c'. After
you changed it, what's the largest submatrix with the same letters you can
make?
Input
The input contains multiple test cases. Each test case
begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a
matrix in row-major order on m lines each with n letters. The input ends once
EOF is met.
Output
For each test case, output one line containing the
number of elements of the largest submatrix of all same letters.
Sample Input
2 4
abcw
wxyz
Sample Output
3
Source
Recommend
指定字母可以全部变为某指定字母
问相同字母组成的最大子矩阵
hdu 1505 加强版
#include<cstdio> #include<cstring> #include<algorithm> #define N 1001 using namespace std; int n,m,k,minn,ans; char s[N+2][N+2],t[N+2][N+2]; int l[N],r[N]; int a[N],b[N]; int q[N],tmp[N],head,tail; void change(char w,char x,char y,char z) { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(s[i][j]==x || s[i][j]==y || s[i][j]==z) t[i][j]=w; else t[i][j]=s[i][j]; } void monotonous(int *c,int *d) { int h=1; while(h<=m && !c[h]) h++; if(h>m) return; q[0]=c[h]; tmp[0]=h; head=0; tail=1; for(int i=h+1;i<=m;i++) { if(!c[i]) while(head<tail) d[tmp[head++]]=i-1; else if(head==tail) q[tail]=c[i],tmp[tail++]=i; else { while(head<tail && c[i]<q[tail-1]) d[tmp[--tail]]=i-1; q[tail]=c[i]; tmp[tail++]=i; } } while(head<tail) d[tmp[head++]]=tmp[tail-1]; } void solve(char x) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(t[i][j]!=x) a[j]=b[m-j+1]=0; else { if(t[i][j]==t[i-1][j]) a[j]++,b[m-j+1]++; else a[j]=b[m-j+1]=1; } } monotonous(a,r); monotonous(b,l); for(int j=1;j<=m;j++) tmp[j]=l[j]; for(int j=1;j<=m;j++) l[m-j+1]=m-tmp[j]+1; for(int j=1;j<=m;j++) ans=max(ans,(r[j]-l[j]+1)*a[j]); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { ans=0; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); change('a','w','y','z'); solve('a'); change('b','w','x','z'); solve('b'); change('c','x','y','z'); solve('c'); printf("%d ",ans); } }