zoukankan      html  css  js  c++  java
  • Largest Submatrix(动态规划)

    Largest Submatrix

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

    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

    题解:

    上题 的加强版。

    三种情况。

    全部变为a,全部为b,全部为c,分别求最大。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<vector>
     7 using namespace std;
     8 const int INF=0x3f3f3f3f;
     9 #define mem(x,y) memset(x,y,sizeof(x))
    10 #define SI(x) scanf("%d",&x)
    11 #define PI(x) printf("%d",x)
    12 #define SD(x,y) scanf("%lf%lf",&x,&y)
    13 #define P_ printf(" ")
    14 const int MAXN=1010;
    15 typedef long long LL;
    16 int dp[3][MAXN][MAXN],s[MAXN],l[MAXN],r[MAXN];
    17 char mp[MAXN][MAXN];
    18 bool isa(char ch){
    19     if(ch=='a'||ch=='w'||ch=='y'||ch=='z')
    20         return true;
    21     else return false;
    22 }
    23 bool isb(char ch){
    24     if(ch=='b'||ch=='w'||ch=='x'||ch=='z')
    25         return true;
    26     else return false;
    27 }
    28 bool isc(char ch){
    29     if(ch=='c'||ch=='x'||ch=='y'||ch=='z')
    30         return true;
    31     else return false;
    32 }
    33 
    34 int main(){
    35     int N,M;
    36     while(~scanf("%d%d",&N,&M)){
    37         for(int i=1;i<=N;i++)
    38             scanf("%s",mp[i]+1);
    39             mem(dp,0);
    40             int ans=0;
    41         for(int i=1;i<=N;i++){
    42             for(int j=1;mp[i][j];j++){
    43                 if(isa(mp[i][j]))dp[0][i][j]=dp[0][i-1][j]+1;
    44                 s[j]=dp[0][i][j];l[j]=j;r[j]=j;
    45             }
    46             s[0]=s[M+1]=-1;
    47             for(int j=1;j<=M;j++){
    48                 while(s[l[j]-1]>=s[j])
    49                     l[j]=l[l[j]-1];
    50             }
    51             for(int j=M;j>=1;j--){
    52                 while(s[r[j]+1]>=s[j])
    53                     r[j]=r[r[j]+1];
    54             }
    55             for(int j=1;j<=M;j++){
    56                 ans=max((r[j]-l[j]+1)*s[j],ans);
    57             }
    58             //
    59             for(int j=1;mp[i][j];j++){
    60                 if(isb(mp[i][j]))dp[1][i][j]=dp[1][i-1][j]+1;
    61                 s[j]=dp[1][i][j];l[j]=j;r[j]=j;
    62             }
    63             s[0]=s[M+1]=-1;
    64             for(int j=1;j<=M;j++){
    65                 while(s[l[j]-1]>=s[j])
    66                     l[j]=l[l[j]-1];
    67             }
    68             for(int j=M;j>=1;j--){
    69                 while(s[r[j]+1]>=s[j])
    70                     r[j]=r[r[j]+1];
    71             }
    72             for(int j=1;j<=M;j++){
    73                 ans=max((r[j]-l[j]+1)*s[j],ans);
    74             }
    75             //
    76             for(int j=1;mp[i][j];j++){
    77                 if(isc(mp[i][j]))dp[2][i][j]=dp[2][i-1][j]+1;
    78                 s[j]=dp[2][i][j];l[j]=j;r[j]=j;
    79             }
    80             s[0]=s[M+1]=-1;
    81             for(int j=1;j<=M;j++){
    82                 while(s[l[j]-1]>=s[j])
    83                     l[j]=l[l[j]-1];
    84             }
    85             for(int j=M;j>=1;j--){
    86                 while(s[r[j]+1]>=s[j])
    87                     r[j]=r[r[j]+1];
    88             }
    89             for(int j=1;j<=M;j++){
    90                 ans=max((r[j]-l[j]+1)*s[j],ans);
    91             }
    92         }
    93         printf("%d
    ",ans);
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    在vs2008中集成JavaScript Lint检查JavaScript语法
    (转载)SQL分页语句
    设置出错页
    判断2个输入框至少输入1个
    C#获取用户桌面等特殊系统路径
    创建存储过程的代码
    SqlParameter关于Like的传参数无效问题
    (转)利用Office里面的OWC组件进行画图
    firefox3不能获得html file的全路径的问题
    (转)使用ASP.NET上传图片汇总
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5204180.html
Copyright © 2011-2022 走看看