HDU-2859-Phalanx(DP)

链接:

https://vjudge.net/problem/HDU-2859

题意:

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 nn, 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

思路:

考虑对角线, DP[i, j]由DP[i-1, j+1]更新而来, 对于(i, j)位置, 上方和右方扩展, 找到最长距离和Dp[i-1, j+1]进行对比更新.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 1e3+10;

char Map[MAXN][MAXN];
int Dp[MAXN][MAXN];
int n;

int main()
{
    while (scanf("%d", &n) && n)
    {
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%s", Map[i]+1);
        memset(Dp, 0, sizeof(Dp));
        int res = 1;
        for (int i = 1;i <= n;i++)
            Dp[1][i] = 1;
        for (int i = 2;i <= n;i++)
        {
            for (int j = 1;j <= n;j++)
            {
                int lx = i-1, ry = j+1;
                int cnt = 0;
                while (lx <= n && ry <= n && Map[lx][j] == Map[i][ry])
                    cnt++, lx--, ry++;
                Dp[i][j] = min(Dp[i-1][j+1]+1, cnt+1);
                res = max(Dp[i][j], res);
            }
        }
        printf("%d
", res);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11664215.html