hdu2859Phalanx

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

 题意:问矩阵p中最大的对称矩阵(对称轴是副对角线)边长是多大。

dp[i][j]是p[i][j]作为左下角时的最大对称矩阵 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1010;
 6 int n;
 7 char p[maxn][maxn];
 8 int dp[maxn][maxn];
 9 
10 int main(){
11     while(scanf("%d",&n)&&n){
12         int ans=1;
13         memset(dp,0,sizeof(dp));
14         for(int i=0;i<n;i++)
15             scanf("%s",p[i]);
16         for(int i=0;i<n;i++){
17             for(int j=0;j<n;j++){
18                 if(i==0||j==n-1){   //第一行和最后一列初始为1,因为无法继续延伸
19                     dp[i][j]=1;
20                     continue;
21                 }
22                 int t1=i,t2=j;
23                 while(t1>=0&&t2<n&&p[t1][j]==p[i][t2]){  //从(i,j)分别向上向右延伸,直到不相等为止
24                     t1--;
25                     t2++;
26                 }
27                 int k=i-t1;
28                 dp[i][j]=min(dp[i-1][j+1]+1,k);
29                 ans=max(ans,dp[i][j]);  //最大值
30             }
31         }
32         printf("%d
",ans);
33     }
34     return 0;
35 }
View Code
原文地址:https://www.cnblogs.com/yijiull/p/6646724.html