• 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 }
  • 相关阅读:
    假期每日小结_2.2
    假期每日小结_2.1
    《新浪微博用户兴趣建模系统架构》阅读笔记
    《微博深度学习平台架构和实践》阅读笔记
    《亿级用户下的新浪微博平台架构》阅读笔记
    JavaScript中JSON的序列化和解析
    Servlet中@WebServlet("XXXX")注解无效,访问servlet报404错误
    数据卷(Data Volumes)
    Docker安装及基本命令
    springcloud服务配置中心
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5204180.html
Copyright © 2020-2023  润新知