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\n",ans);
94     }
95     return 0;
96 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值