【DP】HDU2870 Largest Submatrix

来源:互联网 发布:淘宝联盟靠谱吗 编辑:程序博客网 时间:2024/04/27 20:54

Largest Submatrix
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 

题目意思就是找出同一字母的最大面积的矩形,最开始我理解错了题目,以为是最大连在一起的同一字母面积,不一定是矩形,结果果断WA……理解题意后想了很久都没思路。仍然百度,发现这题是HDU1505的加强版,而HDU1505是HDU1506的加强版哭,这样做一题顶三题了……

好吧,废话少说,这题的做法是,采用寻找最大矩阵的方法做三遍。而寻找最大矩阵的办法是先创建一个数组num记录每个节点的高度(即同一字母堆叠起来的高度),创建两个数组分别记录以每个节点高度为最大高度的矩阵的长度,然后依次高度乘以长度,找出最大的即可。怎么有种LTS的感觉……

代码如下:

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;int num[1005],l[1005],r[1005];char map[1005][1005],mp[1005][1005];int max(int a,int b,int c){    a=a>b?a:b;    a=a>c?a:c;    return a;}int main(){    int n,m,i,j,ma=0,mb=0,mc=0;    while(~scanf("%d%d",&n,&m))    {        ma=0;mb=0;mc=0;        for(i=1;i<=n;i++)        for(j=1;j<=m;j++)            cin>>map[i][j];        memset(num,0,sizeof(num));        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)           {            if(map[i][j]=='w'||map[i][j]=='y'||map[i][j]=='z')            mp[i][j]='a';            else            mp[i][j]=map[i][j];            if(mp[i][j]=='a')            num[j]+=1;            else            num[j]=0;            l[j]=j;            r[j]=j;           }           num[0]=num[m+1]=-1;           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k-1]&&num[j]>0)               {                   k=l[k-1];               }               l[j]=k;           }           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k+1]&&num[j]>0)               {                   k=r[k+1];               }               r[j]=k;           }           for(j=1;j<=m;j++)           {               int temp=num[j]*(r[j]-l[j]+1);               if(ma<temp)               ma=temp;           }        }        memset(num,0,sizeof(num));        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)           {            if(map[i][j]=='w'||map[i][j]=='x'||map[i][j]=='z')            mp[i][j]='b';            else            mp[i][j]=map[i][j];            if(mp[i][j]=='b')            num[j]+=1;            else            num[j]=0;            l[j]=j;            r[j]=j;           }           num[0]=num[m+1]=-1;           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k-1]&&num[j]>0)               k=l[k-1];               l[j]=k;           }           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k+1]&&num[j]>0)               k=r[k+1];               r[j]=k;           }           for(j=1;j<=m;j++)           {               int temp=num[j]*(r[j]-l[j]+1);               if(mb<temp)               mb=temp;           }        }        memset(num,0,sizeof(num));        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)           {            if(map[i][j]=='x'||map[i][j]=='y'||map[i][j]=='z')            mp[i][j]='c';            else            mp[i][j]=map[i][j];            if(mp[i][j]=='c')            num[j]+=1;            else            num[j]=0;            l[j]=j;            r[j]=j;           }           num[0]=num[m+1]=-1;           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k-1]&&num[j]>0)               k=l[k-1];               l[j]=k;           }           for(j=1;j<=m;j++)           {               int k=j;               while(num[j]<=num[k+1]&&num[j]>0)               k=r[k+1];               r[j]=k;           }           for(j=1;j<=m;j++)           {               int temp=num[j]*(r[j]-l[j]+1);               if(mc<temp)               mc=temp;           }        }     cout<<max(ma,mb,mc)<<endl;    }    return 0;}


0 0