HDU OJ Largest Submatrix
Largest Submatrix
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
分析:将w,x,y,z全部转化为a,b,c,然后分别求关于a,b,c,的最大子矩阵,tempa,tempb,tempc,三者的最大值即为结果。
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
int numa[1005],numb[1005],numc[1005];
int Maxa,Maxb,Maxc;
int Mina,Minb,Minc;
int result;
char ch;
int m,n;
int bit[30];
int counta,countb,countc,tempa,tempb,tempc;
int main()
{
int i,j,k;
bit[0]=4;
bit[1]=2;
bit[2]=1;
bit[22]=6;
bit[23]=3;
bit[24]=5;
bit[25]=7;
while(cin>>m>>n)
{
getchar();
memset(numa,0,sizeof(numa));
memset(numb,0,sizeof(numb));
memset(numc,0,sizeof(numc));
tempa=tempb=tempc=0;
for(i=0;i<m;i++)
{
Maxa=Maxb=Maxc=-1;
Mina=Minb=Minc=0x7fffffff;
for(j=0;j<n;j++)
{
ch=getchar();
if((bit[ch-'a']&4)!=0)
{
numa[j]++;
}
else
{
numa[j]=0;
}
Maxa=max(numa[j],Maxa);
Mina=min(Mina,numa[j]);
if((bit[ch-'a']&2)!=0)
{
numb[j]++;
}
else
{
numb[j]=0;
}
Maxb=max(numb[j],Maxb);
Minb=min(Minb,numb[j]);
if((bit[ch-'a']&1)!=0)
{
numc[j]++;
}
else
{
numc[j]=0;
}
Maxc=max(numc[j],Maxc);
Minc=min(Minc,numc[j]);
}
getchar();
for(j=Mina;j<=Maxa;j++)
{
counta=0;
for(k=0;k<n;k++)
{
if(j<=numa[k])
{
counta+=j;
tempa=max(counta,tempa);
}
else
{
counta=0;
}
}
}
for(j=Minb;j<=Maxb;j++)
{
countb=0;
for(k=0;k<n;k++)
{
if(j<=numb[k])
{
countb+=j;
tempb=max(countb,tempb);
}
else
{
countb=0;
}
}
}
for(j=Minc;j<=Maxc;j++)
{
countc=0;
for(k=0;k<n;k++)
{
if(j<=numc[k])
{
countc+=j;
tempc=max(countc,tempc);
}
else
{
countc=0;
}
}
}
}
result=max(tempa,max(tempb,tempc));
cout<<result<<endl;
}
return 0;
}
浙公网安备 33010602011771号