HDOJ题目2870 Largest Submatrix(动态规划)

来源:互联网 发布:商标域名注册管理局 编辑:程序博客网 时间:2024/04/30 12:33

Largest Submatrix

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


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 4abcwwxyz
 

Sample Output
3
 

Source
2009 Multi-University Training Contest 7 - Host by FZU
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2830 2577 1978 1231 1159 
 
ac代码
#include<stdio.h>#include<string.h>int dp[1010][1010],l[1010],r[1010];char s[1010][1010];int n,m,max;void fun(){int i,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){l[j]=r[j]=j;}dp[i][0]=dp[i][m+1]=-1;for(j=2;j<=m;j++){while(dp[i][j]<=dp[i][l[j]-1])l[j]=l[l[j]-1];}for(j=m-1;j>=1;j--){while(dp[i][j]<=dp[i][r[j]+1])r[j]=r[r[j]+1];}for(j=1;j<=m;j++){int res=(r[j]-l[j]+1)*dp[i][j];if(res>max)max=res;}}}int main(){//int n,m;while(scanf("%d%d",&n,&m)!=EOF){int i,j;max=0;for(i=1;i<=n;i++){scanf("%s",s[i]);}for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(s[i][j]=='a'||s[i][j]=='w'||s[i][j]=='y'||s[i][j]=='z'){dp[i][j]=dp[i-1][j]+1;}elsedp[i][j]=0;}}fun();for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(s[i][j]=='b'||s[i][j]=='w'||s[i][j]=='x'||s[i][j]=='z'){dp[i][j]=dp[i-1][j]+1;}elsedp[i][j]=0;}}fun();for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(s[i][j]=='c'||s[i][j]=='y'||s[i][j]=='x'||s[i][j]=='z'){dp[i][j]=dp[i-1][j]+1;}elsedp[i][j]=0;}}fun();printf("%d\n",max);}}


0 0
原创粉丝点击