zoukankan      html  css  js  c++  java
  • HDU2870 Largest Submatrix

    Largest Submatrix

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


    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
     

    Source
     

    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  2830 2577 1505 2845 1069 
     

    Statistic | Submit | Discuss | Note

    有个字母矩阵,包含字母"a、b、c、w、x、y、z",其中,w能变为"a、b",x能变为"b、c",y能变为"a、c",z能变为"a、b、c"。问能构成的最大字母完全一样的子矩阵面积为多大?

    对三种字符分别考虑,每次尽量转换成一种字符。

    问题就变成了最大01子矩阵。上单调栈即可。

    时间复杂度(O(mn))

    #include<iostream>
    #define rg register
    #define il inline
    #define co const
    template<class T>il T read(){
        rg T data=0,w=1;rg char ch=getchar();
        while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
        while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
        return data*w;
    }
    template<class T>il T read(rg T&x) {return x=read<T>();}
    typedef long long ll;
    using namespace std;
    
    co int N=1002;
    int m,n,a[N][N],b[N][N],c[N][N],s[N],w[N];
    char buf[N];
    void Largest_Submatrix(){
    	for(int i=1;i<=m;++i){
    		scanf("%s",buf+1);
    		for(int j=1;j<=n;++j){
    			if(buf[j]=='a'||buf[j]=='w'||buf[j]=='y'||buf[j]=='z') a[i][j]=a[i-1][j]+1;
    			else a[i][j]=0;
    			if(buf[j]=='b'||buf[j]=='w'||buf[j]=='x'||buf[j]=='z') b[i][j]=b[i-1][j]+1;
    			else b[i][j]=0;
    			if(buf[j]=='c'||buf[j]=='x'||buf[j]=='y'||buf[j]=='z') c[i][j]=c[i-1][j]+1;
    			else c[i][j]=0;
    		}
    	}
    	int ans=0;
    	for(int i=1;i<=m;++i){
    // max of a
    		int p=0;
    		for(int j=1;j<=n+1;++j){
    			if(a[i][j]>s[p]) s[++p]=a[i][j],w[p]=1;
    			else{
    				int width=0;
    				while(s[p]>a[i][j]){
    					width+=w[p];
    					ans=max(ans,width*s[p]);
    					--p;
    				}
    				s[++p]=a[i][j],w[p]=width+1;
    			}
    		}
    // max of b
    		p=0;
    		for(int j=1;j<=n+1;++j){
    			if(b[i][j]>s[p]) s[++p]=b[i][j],w[p]=1;
    			else{
    				int width=0;
    				while(s[p]>b[i][j]){
    					width+=w[p];
    					ans=max(ans,width*s[p]);
    					--p;
    				}
    				s[++p]=b[i][j],w[p]=width+1;
    			}
    		}
    // max of c
    		p=0;
    		for(int j=1;j<=n+1;++j){
    			if(c[i][j]>s[p]) s[++p]=c[i][j],w[p]=1;
    			else{
    				int width=0;
    				while(s[p]>c[i][j]){
    					width+=w[p];
    					ans=max(ans,width*s[p]);
    					--p;
    				}
    				s[++p]=c[i][j],w[p]=width+1;
    			}
    		}
    	}
    	printf("%d
    ",ans);
    }
    int main(){
    	while(~scanf("%d %d",&m,&n)) Largest_Submatrix();
    	return 0;
    }
    
  • 相关阅读:
    阿里云云效技术专家分享:云原生开发、调测及可靠发布解决方案
    对话李飞飞,揭秘国际体育赛事风“云”背后的黑科技
    时序数据库永远的难关 — 时间线膨胀(高基数 Cardinality)问题的解决方案
    当Java遇上机密计算,又一段奇幻之旅开始了!
    内核热补丁,真的安全么?
    在 Dubbo3.0 上服务治理的实践
    CCF-201509-3-生成模板系统
    WPF CommandParameter的使用
    UWP App Data存储和获取
    在WPF中的ItemsControl中使用事件和命令(Using events and Commands within ItemsControl in WPF)
  • 原文地址:https://www.cnblogs.com/autoint/p/10802647.html
Copyright © 2011-2022 走看看