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;
    }
    
  • 相关阅读:
    【纪中集训】2019.08.01【NOIP提高组】模拟 A 组TJ
    有上下界的网络流 学习笔记
    平衡规划后的华丽暴力——莫队算法
    【纪中集训】2019.07.11【NOIP提高组】模拟 B 组TJ
    hdu 2063 过山车 二分匹配(匈牙利算法)
    hdu 5373 The shortest problem(杭电多校赛第七场)
    hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
    hdu 2119 Matrix(二分匹配)
    hdu 1498 50 years, 50 colors(二分匹配_匈牙利算法)
    HNU Joke with permutation (深搜dfs)
  • 原文地址:https://www.cnblogs.com/autoint/p/10802647.html
Copyright © 2011-2022 走看看