HDU 2870 Largest Submatrix 最大完全子矩阵之不可移动列+中间元素可改变

Largest Submatrix

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


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
 
代码:

#include<stdio.h>
#include<string.h>
#define N 1005
#define MX(a,b) (a>b?a:b)
int num[N];
char s[N][N];
int n,m;

bool check(char c,char ss)
{
   if(c=='a')
   return ss=='a'||ss=='w'||ss=='y'||ss=='z';
   if(c=='b')
   return ss=='b'||ss=='w'||ss=='x'||ss=='z';
   if(c=='c')
   return ss=='c'||ss=='x'||ss=='y'||ss=='z';  
}

int main()
{
   while(scanf("%d%d",&n,&m)!=-1)
   {
      for(int i=0;i<n;i++)
   scanf("%s",s[i]);
   int max=0;
   for(char c='a';c<='c';c++)
   {
      memset(num,0,sizeof(num));
   for(int i=0;i<n;i++)
   {
      for(int j=0;j<m;j++)
   {
      if(check(c,s[i][j]))
      num[j]++;
      else
      num[j]=0;   
      }   
      for(int j=0;j<m;j++)
      {
        int cnt=1;
        if(!num[j])
        continue;

      //枚举每个高度,然后以它做基准,向左向右扩张,扩张条件是h[i-k]>=h[k]、h[i+k]>=h[k]。记录扩张的个数即可。
        for(int k=1;j-k>=0&&num[j-k]>=num[j];k++)
        cnt++;
        for(int k=1;j+k<m&&num[j+k]>=num[j];k++ )
        cnt++;
        max=MX(max,cnt*num[j]);
       }
         }     
   }       
   printf("%d\n",max);
   }  
   return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870

转载于:https://www.cnblogs.com/hebozi/archive/2012/08/11/2633470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值