HDU2859 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

InputThere 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.OutputEach 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

题意:
问最大对称矩阵,对称轴是这样的:/
思路:
dp[i][j]表示以i,j为左下角坐标的最大矩阵大小。
更新的时候向上和向右走就行了,只是感觉这个复杂度不太正常。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
char mp[1024][1024];
int dp[1024][1024];
int main()
{
    int n;
    int ans;
    while(scanf("%d",&n)&&n){
        ans=0;
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                int t1=i,t2=j;
                int m=dp[i-1][j+1];
                int rec=1;
                for(int k=1;k<=m+1;k++){
                    t1--;t2++;
                    if(t1<=0||t2>n){break;}
                    if(mp[t1][j]==mp[i][t2]){rec++;}
                    else break;
                }
                dp[i][j]=1;
                if(rec>dp[i-1][j+1]){dp[i][j]=dp[i-1][j+1]+1;ans=max(ans,dp[i][j]);}
                else dp[i][j]=rec;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 



posted @ 2019-04-07 11:43  断腿三郎  阅读(197)  评论(0编辑  收藏  举报