zoukankan      html  css  js  c++  java
  • hdu2870之DP

    Largest Submatrix

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

    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
     
    分析:对于第i行第j列,判断从上到下连接的高度,然后判断这个点能延伸的最左端和最右端,延伸的条件是左端/右端字母相等且高度大于等于这个点
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<iomanip>
    #define INF 99999999
    using namespace std;
    
    const int MAX=1000+10;
    int Left[MAX],Right[MAX];//记录比当前行在位置i可连接字母高于等于hight[i]的最左端和最右端
    int hight[2][MAX];//hight[i]记录每一列从上到下到当前行的高(即连在一起相同字母的个数)
    int n,m;
    char s[MAX][MAX],Map[26][3];//Map映射a,b,c,w,x,y,z的转换结果 
    
    int MaxMatrix(){
    	int sum=0;
    	for(int k=0;k<3;++k){//转换成哪种字母 
    		for(int i=0;i<n;++i){
    			for(int j=0;j<m;++j){//求hight 
    				if(i == 0)hight[0][j]=1;
    				else hight[i%2][j]=(Map[s[i][j]-'a'][k] == Map[s[i-1][j]-'a'][k]) ? hight[(i-1)%2][j]+1 : 1;
    				Left[j]=Right[j]=j;
    			}
    			for(int j=1;j<m;++j){//求Left 
    				while(Left[j]-1>=0 && hight[i%2][Left[j]-1]>=hight[i%2][j] && 
    					  Map[s[i][Left[j]-1]-'a'][k] == Map[s[i][Left[j]]-'a'][k])Left[j]=Left[Left[j]-1];
    			}
    			for(int j=m-2;j>=0;--j){//求Right
    				while(Right[j]+1<m && hight[i%2][Right[j]+1]>=hight[i%2][j] && 
    					  Map[s[i][Right[j]+1]-'a'][k] == Map[s[i][Right[j]]-'a'][k])Right[j]=Right[Right[j]+1];
    			}
    			for(int j=0;j<m;++j){
    				sum=max(sum,(Right[j]-Left[j]+1)*hight[i%2][j]);
    			}
    		}
    	}
    	return sum;
    }
    
    int main(){
    	for(int i=0;i<3;++i)for(int j=0;j<3;++j)Map[i][j]=i+'a';
    	Map['w'-'a'][0]='a',Map['w'-'a'][1]='b',Map['w'-'a'][2]='w';
    	Map['x'-'a'][0]='x',Map['x'-'a'][1]='b',Map['x'-'a'][2]='c';
    	Map['y'-'a'][0]='a',Map['y'-'a'][1]='y',Map['y'-'a'][2]='c';
    	Map['z'-'a'][0]='a',Map['z'-'a'][1]='b',Map['z'-'a'][2]='c';
    	while(~scanf("%d%d",&n,&m)){
    		for(int i=0;i<n;++i)scanf("%s",s+i);
    		printf("%d
    ",MaxMatrix());
    	}
    	return 0;
    }


  • 相关阅读:
    迭代器模式 -- 大话设计模式
    组合模式 -- 大话设计模式
    备忘录模式 -- 大话设计模式
    totalcmd简单教程--help详解
    Listary Primary
    Cygwin Primary
    Google calendar
    极点郑码标点符号
    Totalcmd 简单教程
    Foobar 简单教程
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3265389.html
Copyright © 2011-2022 走看看