HDU2859 Phalanx (动态规划)

时间:2022-11-07 06:00:07
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 = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
char mp[][];
int dp[][];
int main()
{
int n;
int ans;
while(scanf("%d",&n)&&n){
ans=;
for(int i=;i<=n;i++){
scanf("%s",mp[i]+);
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int t1=i,t2=j;
int m=dp[i-][j+];
int rec=;
for(int k=;k<=m+;k++){
t1--;t2++;
if(t1<=||t2>n){break;}
if(mp[t1][j]==mp[i][t2]){rec++;}
else break;
}
dp[i][j]=;
if(rec>dp[i-][j+]){dp[i][j]=dp[i-][j+]+;ans=max(ans,dp[i][j]);}
else dp[i][j]=rec;
}
}
printf("%d\n",ans);
}
return ;
}