HDU_2859_Phalanx

来源:互联网 发布:mr消音软件 编辑:程序博客网 时间:2024/06/07 13:09

Phalanx

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2087    Accepted Submission(s): 1027


Problem Description
Today is army day, but the servicemen are busy with the phalanx for the celebration of the 60th anniversary of the PRC.
A phalanx is a matrix of size n*n, each element is a character (a~z or A~Z), standing for the military branch of the servicemen on that position.
For some special requirement it has to find out the size of the max symmetrical sub-array. And with no doubt, the Central Military Committee gave this task to ALPCs.
A symmetrical matrix is such a matrix that it is symmetrical by the “left-down to right-up” line. The element on the corresponding place should be the same. For example, here is a 3*3 symmetrical matrix:
cbx
cpb
zcc
 

Input
There are several test cases in the input file. Each case starts with an integer n (0<n<=1000), followed by n lines which has n character. There won’t be any blank spaces between characters or the end of line. The input file is ended with a 0.
 

Output
Each test case output one line, the size of the maximum symmetrical sub- matrix.
 

Sample Input
3abxcybzca4zabacbababbccacq0
 

Sample Output
33
 

Source

2009 Multi-University Training Contest 5 - Host by NUDT


  • 很朴素的dp
  • 矩阵要求沿副对角线对称求最大矩阵
  • 初始化最上边和最左边为1
  • 对于每个格点,矩阵不大于右上角的值
  • 然后检查对于右上矩阵的以当前格点为左下角的新加的两个边的最大匹配
  • 注意要从当前格点逐渐向末端检查,不然很痛苦,这题有点卡常

#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <cmath>#include <vector>#include <queue>#include <stack>#include <set>#include <map>using namespace std;typedef long long           LL ;typedef unsigned long long ULL ;const int    maxn = 1e3 + 10   ;const int    inf  = 0x3f3f3f3f ;const int    npos = -1         ;const int    mod  = 1e9 + 7    ;const int    mxx  = 100 + 5    ;const double eps  = 1e-6       ;const double PI   = acos(-1.0) ;int n, ans, dp[maxn][maxn];char a[maxn][maxn];int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);while(~scanf("%d",&n) && n){ans=1;for(int i=0;i<n;i++)scanf("%s",a[i]);for(int i=0;i<n;i++)for(int j=0;j<n;j++){if(i==0 || j==n-1){dp[i][j]=1;}else{dp[i][j]=1+dp[i-1][j+1];for(int k=1;k<=dp[i-1][j+1];k++)if(a[i-k][j]!=a[i][j+k]){dp[i][j]=k;break;}}ans=max(ans,dp[i][j]);}printf("%d\n",ans);}return 0;}


原创粉丝点击