Largest Submatrix

来源:互联网 发布:录屏软件fast 编辑:程序博客网 时间:2024/03/29 22:07

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

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  
思路:一开始简直束手无策啊。不知道怎么压缩到1维,后来发现,直接枚举a,b,c取最大值就是答案
然后能换成a,b,c的地方置1,不能换的置0
换成a的有a,w,y,z
换成b的有b,w,x,z
换成c的有c,x,y,z
换成a 原矩阵变为
1 0 0 1
1 0 1 1
前缀和矩阵为
1 0 0 1
2 0 1 2
然后枚举每一行,能求形成的最大矩阵,换成b,c同上
求法:从左到右遍历,求以第i个 sum[i]为高能形成的最大矩阵
用dp的思路 快速找到最左边和最右边最后一个大于等于sum[i]的位置l[i]和r[i]
然后答案就是
再枚举1-i 的max(sum[i]*(r[i]-l[i]+1)) 
这样就求出一行了,然后枚举行求出最大的答案就对了
代码如下: 

//
// Create by 神舟 on 2015-02-13
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;

#define CLR(x) memset(x,0,sizeof x)
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=1e3+5;
const int MOD=5e5+5;
char word[maxn][maxn];
int num[maxn][maxn];
int sum[maxn][maxn];
int l[maxn],a[maxn],r[maxn];
int n,m;

//a=a,w,y,z
//b=b,w,x,z
//c=c,x,y,z
int sol(char ch)//枚举3种a,b,c求最大答案
{
    int ans=0;
    CLR(num);
    CLR(sum);//初始化
    if(ch=='a'){
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){//处理+前缀和
            if(word[i][j]=='a'||word[i][j]=='w'||word[i][j]=='y'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                l[j]=j;
                while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];//dp思想求左边坐标
            }
            for(int j=m;j>0;j--){
                r[j]=j;
                while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];dp思想求右边坐标
            }
            for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));枚举高求出最大值
        }
    }
    else if(ch=='b'){之后代码和上面基本一致
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
            if(word[i][j]=='b'||word[i][j]=='w'||word[i][j]=='x'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    l[j]=j;
                    while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];
                }
                for(int j=m;j>0;j--){
                    r[j]=j;
                    while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];
                }
                for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));
            }
    }
    else{
         for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){
            if(word[i][j]=='c'||word[i][j]=='x'||word[i][j]=='y'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    l[j]=j;
                    while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];
                }
                for(int j=m;j>0;j--){
                    r[j]=j;
                    while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];
                }
                for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));
            }
    }
    return ans;
}

int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%s",&word[i][1]);
        }
        int ans=max(sol('a'),sol('b'));
        ans=max(ans,sol('c'));
        printf("%d\n",ans);
}

return 0;
}
第一次写的这种类型,没考虑空间的优化,然后 这道题可以边输入,边处理,不用开某些数组,空间能得到很大的优化 

0 0