思路:
- 最大对称子矩阵,一开始就有些思路,搜了下题解发现差不多就直接动手写了,A的好开心。
- 算是枚举吧,其实大部分 dp 都是优化的枚举嘛,本题略有看表填格的影子。
- dp[i][j] 代表以 mp[i][j] 为左下对角元的最长对称矩阵边长。写法要注意,本代码写的稍微有一点点取巧。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int N;
char mp[maxn][maxn];
int dp[maxn][maxn];
int SOLVE(){
int MAX = 0;
memset(dp , 0 , sizeof(dp));
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
int x = i , y = j;
while(x>=1 && y<=N){
x-- , y++;
if(mp[x][j] != mp[i][y])
break;
}
int temp = i - x;
dp[i][j] = min(dp[i-1][j+1] + 1 , temp);
MAX = max(MAX , dp[i][j]);
}
}
return MAX;
}
int main(){
while(cin>>N && N){
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
cin>>mp[i][j];
cout<<SOLVE()<<endl;
}
return 0;
}