HDU 2870 Largest Submatrix (求最大子矩阵变形,可变值)枚举每种情况

来源:互联网 发布:最好的大六壬排盘软件 编辑:程序博客网 时间:2024/06/09 06:49

Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1974    Accepted Submission(s): 949


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 4abcwwxyz
 

Sample Output
3
 
题意:
给你n*m个字符,求最大子矩阵(含有相同的字母最多是多少);
w,x,y,z;可以变形成其他字母(见题意);

思路:
将求子矩阵分为3种情况:以全是a的矩阵,全是b的,c的矩阵;
然后进行类似1505的求发就行了;
#include<bits/stdc++.h>using namespace std;int a[1010][1010];int b[1010][1010];int c[1010][1010];char s[1010][1009];int R[1010][1010];int L[1010][1010];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(c,0,sizeof(c));        for(int i=0;i<n;i++)        {            scanf("%s",s[i]);        }        for(int i=n-1;i>=0;i--)        {            for(int j=0;j<m;j++)            {                if(s[i][j]=='a'||s[i][j]=='w'||s[i][j]=='y'||s[i][j]=='z')                a[i][j]=a[i+1][j]+1;                if(s[i][j]=='b'||s[i][j]=='w'||s[i][j]=='x'||s[i][j]=='z')                b[i][j]=b[i+1][j]+1;                if(s[i][j]=='a'||s[i][j]=='x'||s[i][j]=='y'||s[i][j]=='z')                 c[i][j]=c[i+1][j]+1;                 R[i][j]=j;                 L[i][j]=j;            }        }        int Max=-1;        for(int i=0;i<n;i++)        {            for(int j=1;j<m;j++)            {                int t=j-1;                while(a[i][j]<=a[i][t]&&L[i][t]>=0)                {                    L[i][j]=L[i][t];                    t=L[i][j]-1;                    if(t<0) break;                }            }            for(int j=m-2;j>=0;j--)            {                int t=j+1;                while(a[i][j]<=a[i][t]&&R[i][t]>=0)                {                    R[i][j]=R[i][t];                    t=R[i][t]+1;                    if(t>=m)break;                }            }            for(int j=0;j<m;j++)          //求最值            Max=max(Max,(R[i][j]-L[i][j]+1)*a[i][j]);            for(int j=0;j<m;j++)    //初始化            {                L[i][j]=j;                R[i][j]=j;            }        }        //b        for(int i=0;i<n;i++)        {            for(int j=1;j<m;j++)            {                int t=j-1;                while(b[i][j]<=b[i][t]&&L[i][t]>=0)                {                    L[i][j]=L[i][t];                    t=L[i][j]-1;                    if(t<0) break;                }            }            for(int j=m-2;j>=0;j--)            {                int t=j+1;                while(b[i][j]<=b[i][t]&&R[i][t]>=0)                {                    R[i][j]=R[i][t];                    t=R[i][t]+1;                    if(t>=m)break;                }            }            for(int j=0;j<m;j++)          //求最值            Max=max(Max,(R[i][j]-L[i][j]+1)*b[i][j]);            for(int j=0;j<m;j++)    //初始化            {                L[i][j]=j;                R[i][j]=j;            }        }        //c        for(int i=0;i<n;i++)        {            for(int j=1;j<m;j++)            {                int t=j-1;                while(c[i][j]<=c[i][t]&&L[i][t]>=0)                {                    L[i][j]=L[i][t];                    t=L[i][j]-1;                    if(t<0) break;                }            }            for(int j=m-2;j>=0;j--)            {                int t=j+1;                while(c[i][j]<=c[i][t]&&R[i][t]>=0)                {                    R[i][j]=R[i][t];                    t=R[i][t]+1;                    if(t>=m)break;                }            }            for(int j=0;j<m;j++)          //求最值            Max=max(Max,(R[i][j]-L[i][j]+1)*c[i][j]);            for(int j=0;j<m;j++)    //初始化            {                L[i][j]=j;                R[i][j]=j;            }        }        printf("%d\n",Max);    }    return 0;}

1 0