HDU - 2859 Phalanx

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

3
abx
cyb
zca
4
zaba
cbab
abbc
cacq
0

Sample Output

3
3

思路:

     刚开始做这道题的时候,有个地方都是解决不了,看了一下大牛的博客,才把这道题搞明白,自己还是太菜了,orz。

说说我的理解吧,我们用dp[i][j]当时以(i,j)为矩阵左下角的点,向右上最大能组成的对称矩阵长度。这个dp[i][j]是由他右上的矩阵dp[i-1][j+1]推来,如果我们从j行向右和j列向上进行逐一比较,那么如果相等的长度大于dp[i-1][j+1]则说明以当前点向右上的矩阵包含了dp[i-1][j+1]的矩阵,所以要比他的值大1,如果长度小于,那么只能包含dp[i-1][j+1]组成矩阵的一部分,那么他能组成的最大长度,也就是他匹配相等的长度。然后比较,找到最最最大的就行啦。

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>

typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;

using namespace std;

char s[1050][1050];
int dp[1050][1050];

int main()
{
    int res = 1;
    int n;
    while(sca(n) && n)
    {
      res = 1;
      getchar();
      rep(i,0,n)
      {
        scas(s[i]);
      }
      rep(i,0,n)
      {
        dp[0][i] = 1; // 初始化,第一行最大就是他自身
      }
      rep(i,1,n)
      {
        rep(j,0,n)
        {
          int posi = i-1;
          int posj = j+1;
          while(posi>=0 && posj<n && s[posi][j] == s[i][posj])
          {
            posi--;
            posj++;
          }
          int len = posj - j;
          if(len>dp[i-1][j+1])
            dp[i][j] = dp[i-1][j+1] + 1;
          else
            dp[i][j] = len;
          res = max(res,dp[i][j]);
        }

      }

      pri(res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Prince_NYing/article/details/88953740