Largest Submatrix


  Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u  

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  
思路:一开始简直束手无策啊。不知道怎么压缩到1维,后来发现,直接枚举a,b,c取最大值就是答案
然后能换成a,b,c的地方置1,不能换的置0
换成a的有a,w,y,z
换成b的有b,w,x,z
换成c的有c,x,y,z
换成a 原矩阵变为
1 0 0 1
1 0 1 1
前缀和矩阵为
1 0 0 1
2 0 1 2
然后枚举每一行,能求形成的最大矩阵,换成b,c同上
求法:从左到右遍历,求以第i个 sum[i]为高能形成的最大矩阵
用dp的思路 快速找到最左边和最右边最后一个大于等于sum[i]的位置l[i]和r[i]
然后答案就是
再枚举1-i 的max(sum[i]*(r[i]-l[i]+1)) 
这样就求出一行了,然后枚举行求出最大的答案就对了
代码如下: 

//
// Create by 神舟 on 2015-02-13
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;

#define CLR(x) memset(x,0,sizeof x)
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=1e3+5;
const int MOD=5e5+5;
char word[maxn][maxn];
int num[maxn][maxn];
int sum[maxn][maxn];
int l[maxn],a[maxn],r[maxn];
int n,m;

//a=a,w,y,z
//b=b,w,x,z
//c=c,x,y,z
int sol(char ch)//枚举3种a,b,c求最大答案
{
    int ans=0;
    CLR(num);
    CLR(sum);//初始化
    if(ch=='a'){
        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){//处理+前缀和
            if(word[i][j]=='a'||word[i][j]=='w'||word[i][j]=='y'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                l[j]=j;
                while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];//dp思想求左边坐标
            }
            for(int j=m;j>0;j--){
                r[j]=j;
                while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];dp思想求右边坐标
            }
            for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));枚举高求出最大值
        }
    }
    else if(ch=='b'){之后代码和上面基本一致
        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
            if(word[i][j]=='b'||word[i][j]=='w'||word[i][j]=='x'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    l[j]=j;
                    while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];
                }
                for(int j=m;j>0;j--){
                    r[j]=j;
                    while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];
                }
                for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));
            }
    }
    else{
         for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
            if(word[i][j]=='c'||word[i][j]=='x'||word[i][j]=='y'||word[i][j]=='z') num[i][j]=1;
            else num[i][j]=0;
            if(num[i][j]) sum[i][j]=sum[i-1][j]+num[i][j];
            else sum[i][j]=0;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    l[j]=j;
                    while(l[j]>1&&sum[i][j]<=sum[i][l[j]-1]) l[j]=l[l[j]-1];
                }
                for(int j=m;j>0;j--){
                    r[j]=j;
                    while(r[j]<m&&sum[i][j]<=sum[i][r[j]+1]) r[j]=r[r[j]+1];
                }
                for(int j=1;j<=m;j++) ans=max(ans,sum[i][j]*(r[j]-l[j]+1));
            }
    }
    return ans;
}

int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%s",&word[i][1]);
        }
        int ans=max(sol('a'),sol('b'));
        ans=max(ans,sol('c'));
        printf("%d\n",ans);
}

return 0;
}
第一次写的这种类型,没考虑空间的优化,然后 这道题可以边输入,边处理,不用开某些数组,空间能得到很大的优化 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
最新发布
03-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值