• 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;
    }
    
  • 相关阅读:
    什么时候用using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    在picture library中取某一图片的大图、小图
    jetbrain phpstorm 增加或删除一个 live template
    用 monitorix 开启linux图形化监控
    360上传应用|应用宝上传应用
    markdown 自定义一个锚点
    用CKEDITOR 做自助上传的解决方案2
    按键精灵*ff
    按键精灵http.定义getWeb
    markdown 自己搞一个浏览工具
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11664215.html
Copyright © 2020-2023  润新知