HDU 2859 Phalanx(二维DP)

来源:互联网 发布:网络信息安全相关专业 编辑:程序博客网 时间:2024/05/17 01:57

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2859

题意:求最大的对称矩阵

思路:从右上角到左下角进行DP

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1char map1[1010][1010];int dp[1010][1010];int main(){    int n;    while(scanf("%d",&n) && n)    {        memset(dp,0,sizeof(dp));        for(int i=1; i<=n; i++)            scanf("%s",map1[i]+1);        int ans = 1;        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                int x = i,y=j;                int temp = 0;                while(x>=1 && y<=n && map1[x][j] == map1[i][y])                {                    temp++;                    x--;                    y++;                }                if(temp >= dp[i-1][j+1]+1)                    dp[i][j] = dp[i-1][j+1]+1;                else                    dp[i][j] = temp;                ans = max(ans,dp[i][j]);            }        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击