HDU 2870 Largest Submatrix (最大子矩阵)

来源:互联网 发布:如何做数据统计 编辑:程序博客网 时间:2024/05/22 00:20

Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)   

Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 860    Accepted Submission(s): 421

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

 

View Code
  1 /*  2   跟1505类似,只不过要加上枚举,比较麻烦一点而已。  3   首先将可变成a的全部变成a,记录每一层中a左右可达的边界,然后从上到下找出第i层往上能达到的最大矩阵  4   然后同样把可变成b的全部变成b,做同样的步骤,最后再枚举c。求出最大值即使答案。  5   6   375MS    13160K  7   8 #include <iostream>  9 #include <cstdio> 10 #include <cstring> 11 #include <string> 12 #include <algorithm> 13 #define SIZE 1005 14  15 using namespace std; 16  17 int lt[SIZE][SIZE],rt[SIZE][SIZE]; 18 int height[SIZE][SIZE]; 19 int n,m; 20 int ans; 21 string s[SIZE]; 22  23 void cal() 24 { 25     for(int i=1; i<=n; i++) 26         for(int j=1; j<=m; j++) 27             lt[i][j] = rt[i][j] = j; 28     for(int i=1; i<=n; i++) 29         height[i][0] = height[i][m+1] = -1; 30     for(int i=1; i<=n; i++) 31     { 32         for(int j=1; j<=m; j++) 33         { 34             while(height[i][lt[i][j]-1] >= height[i][j]) 35                 lt[i][j] = lt[i][lt[i][j]-1]; 36         } 37     } 38     for(int i=1; i<=n; i++) 39     { 40         for(int j=m; j>=1; j--) 41         { 42             while(height[i][rt[i][j]+1] >= height[i][j]) 43                 rt[i][j] = rt[i][rt[i][j]+1]; 44         } 45     } 46     for(int i=1; i<=n; i++) 47         for(int j=1; j<=m; j++) 48             ans = max(ans,(rt[i][j]-lt[i][j]+1)*height[i][j]); 49 } 50  51 int main() 52 { 53     while(~scanf("%d%d",&n,&m)) 54     { 55         for(int i=1; i<=n; i++) 56             s[i].clear(); 57         string temp; 58         for(int i=1; i<=n; i++) 59         { 60             cin >> temp; 61             s[i] += '0'; 62             s[i] += temp; 63         } 64         ans = 0; 65         for(int i=0; i<=m; i++) 66             height[0][i] = 0; 67         for(int i=1; i<=n; i++) 68         { 69             for(int j=1; j<=m; j++) 70             { 71                 if(s[i][j] == 'a' || s[i][j] == 'w' || s[i][j] == 'y' || s[i][j] == 'z') 72                     height[i][j] = height[i-1][j]+1; 73                 else 74                     height[i][j] = 0; 75             } 76         } 77         cal(); 78         for(int i=1; i<=n; i++) 79         { 80             for(int j=1; j<=m; j++) 81             { 82                 if(s[i][j] == 'b' || s[i][j] == 'w' || s[i][j] == 'x' || s[i][j] == 'z') 83                      height[i][j] = height[i-1][j]+1; 84                 else 85                     height[i][j] = 0; 86             } 87         } 88         cal(); 89         for(int i=1; i<=n; i++) 90         { 91             for(int j=1; j<=m; j++) 92             { 93                 if(s[i][j] == 'c' || s[i][j] == 'y' || s[i][j] == 'x' || s[i][j] == 'z') 94                      height[i][j] = height[i-1][j]+1; 95                 else 96                     height[i][j] = 0; 97             } 98         } 99         cal();100         printf("%d\n",ans);101     }102     return 0;103 }104 */105 106 /*107   可将lt[][],rt[][]降维成lt[],rt[],循环每一层的时候顺便求出最大值,节省了一半空间消耗108   343MS    5272K109   */110 #include <iostream>111 #include <cstdio>112 #include <cstring>113 #include <string>114 #include <algorithm>115 #define SIZE 1005116 117 using namespace std;118 119 int lt[SIZE],rt[SIZE];120 int height[SIZE][SIZE];121 int n,m;122 int ans;123 string s[SIZE];124 125 void cal()126 {127     for(int i=1; i<=n; i++)128     {129         for(int j=1; j<=m; j++)130             lt[j] = rt[j] = j;131         height[i][0] = height[i][m+1] = -1;132         for(int j=1; j<=m; j++)133         {134             while(height[i][lt[j]-1] >= height[i][j])135                 lt[j] = lt[lt[j]-1];136         }137         for(int j=m; j>=1; j--)138         {139             while(height[i][rt[j]+1] >= height[i][j])140                 rt[j] = rt[rt[j]+1];141         }142         for(int j=1; j<=m; j++)143             ans = max(ans,(rt[j]-lt[j]+1)*height[i][j]);144     }145 }146 147 int main()148 {149     while(~scanf("%d%d",&n,&m))150     {151         for(int i=1; i<=n; i++)152             s[i].clear();153         string temp;154         for(int i=1; i<=n; i++)155         {156             cin >> temp;157             s[i] += '0';158             s[i] += temp;159         }160         ans = 0;161         for(int i=0; i<=m; i++)162             height[0][i] = 0;163         for(int i=1; i<=n; i++)164         {165             for(int j=1; j<=m; j++)166             {167                 if(s[i][j] == 'a' || s[i][j] == 'w' || s[i][j] == 'y' || s[i][j] == 'z')168                     height[i][j] = height[i-1][j]+1;169                 else170                     height[i][j] = 0;171             }172         }173         cal();174         for(int i=1; i<=n; i++)175         {176             for(int j=1; j<=m; j++)177             {178                 if(s[i][j] == 'b' || s[i][j] == 'w' || s[i][j] == 'x' || s[i][j] == 'z')179                      height[i][j] = height[i-1][j]+1;180                 else181                     height[i][j] = 0;182             }183         }184         cal();185         for(int i=1; i<=n; i++)186         {187             for(int j=1; j<=m; j++)188             {189                 if(s[i][j] == 'c' || s[i][j] == 'y' || s[i][j] == 'x' || s[i][j] == 'z')190                      height[i][j] = height[i-1][j]+1;191                 else192                     height[i][j] = 0;193             }194         }195         cal();196         printf("%d\n",ans);197     }198     return 0;199 }

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电话打开后页面上没有东西怎么办 WPS在电脑安装后卸载不了怎么办 ps总要以管理员的身份打开怎么办 3d关的慢保存慢怎么办 无法与服务器建立可靠的连接怎么办 被抵押的房子开发商不解押怎么办 手机系统语言是英文没有中文怎么办 w7主机网插口灯不亮了没网怎么办 电脑用了5年变得很卡了怎么办 苹果6s系统占12g怎么办 百度网盘下载的压缩包打不开怎么办 三星手机微信安装包解析错误怎么办 下身流出来的东西有异味很重怎么办 鞋脚底总有一股酸臭的味道怎么办 腋窝总是有股酸臭的味道应该怎么办 房间里总是有灰尘的味道怎么办 香水喷多了衣服味道太呛怎么办 香水喷在衣服上变黄了怎么办 84喷在白衣服上变红怎么办 夏季高温多肉山地玫瑰砍头了怎么办 海水缸爆藻有些石头不出绿藻怎么办 不小心把b原来的壁纸删了怎么办 注册简历时没有固定电话必填怎么办 申诉找回微信账号密码验证码怎么办 激活淘宝时显示注册账号过多怎么办 科学上网之后电脑上不了网怎么办 阴阳师地域鬼王定位在非洲怎么办 网吧忘记下机有小孩玩我号怎么办 等了三年老公还是不跟小三断怎么办 小三生孩子老公为责任断不了怎么办 没离婚去医院生孩子刷身份证怎么办 小孩早晨起床鼻子痒有鼻涕怎么办 孩子鼻梁上方和眼眶磕肿了怎么办 宝宝开空调冻的流鼻涕打喷嚏怎么办 狼人杀带耳机麦没声音怎么办 王者荣耀边境突围使刘邦大招怎么办 如果有个恶毒又有心机的后妈怎么办 qq炫舞的好忘了怎么办 百度网盘别人发我一个链接我怎么办 感觉地下和墙壁里有蚂蚁怎么办? 射像头监控摄像头家用卡满了怎么办