您的位置:首页 > 其它

hdu 2870 Largest Submatrix(dp)

2015-06-02 18:41 323 查看


Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1856 Accepted Submission(s): 879



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




因为 wxyz可以转化为abc 所以我们就可以考虑 都转化为abc

除了行列之外 还要找出相同的字符 所以这是一个三维的关系

根据字符是否相同的情况 求出每个位置上值的关系

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

char mp[1010][1010];
int dp[1010][1010][5];
int l[1010],r[1010];
char ch[7]={'a','b','c','w','x','y','z'};
int a[7][3]={{1,0,0},{0,1,0},{0,0,1},{1,1,0},{0,1,1},{1,0,1},{1,1,1}};
int mx;
int n,m;

void cal(int i)
{
    int t;
    for(int q=0;q<3;q++)
    {
        l[1]=1;r[m]=m;
        for(int j=2;j<=m;j++)
        {
            t=j;
            while(t>1&&dp[i][j][q]<=dp[i][t-1][q])
                t=l[t-1];
            l[j]=t;
        }
        for(int j=m-1;j>=1;j--)
        {
            t=j;
            while(t<m&&dp[i][j][q]<=dp[i][t+1][q])
                t=r[t+1];
            r[j]=t;
        }
        for(int j=1;j<=m;j++)
            mx=max(mx,(r[j]-l[j]+1)*dp[i][j][q]);
    }
}

int main()
{
//    fread;

    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(int i=1;i<=n;i++)
            scanf("%s",mp[i]+1);
        MEM(dp,0);
        mx=0;
        for(int i=1;i<=n;i++)
        {
//            bug;
            for(int j=1;j<=m;j++)
            {
                for(int k=0;k<7;k++)
                {
                    if(mp[i][j]==ch[k])
                    {
                        for(int q=0;q<3;q++)
                        {
                            if(a[k][q]==1)
                                dp[i][j][q]=dp[i-1][j][q]+1;
                            else dp[i][j][q]=0;
                        }
//                        break;
                    }
                }
            }
            cal(i);
//            bug;
        }
        printf("%d\n",mx);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: