【dp每日一題】HDU 2859 Phalanx
阿新 • • 發佈:2020-12-30
HDU 2859 Phalanx
大意:
給出一個矩陣,要求輸出最大的 延左下到右上的對角線對稱的 矩陣大小
思路:
\(now[i][j]\)代表以\((i,j)\)為左上角的長度為k矩陣是否對稱,那麼它可以由\(pre[i-1][j]\)和\(pre[i][j-1]\)轉移過來,\(pre[i][j]\)代表以\((i,j)\)為左上角的長度為k-1的矩陣是否對稱,所以如果\(pre[i-1][j]\)和\(pre[i][j-1]\)均為true,那麼只需要\(a[i][j]==a[i+k-1][j+k-1]\)即可
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; typedef long long LL; int n, pre[N][N], now[N][N]; char a[N][N]; int main() { while (cin >> n && n != 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; pre[i][j] = 1; } } int res = 1; for (int k = 2; k <= n; k++) { int flag = 0; for (int i = 0; i <= n - k; i++) { for (int j = 0; j <= n - k; j++) { if (a[i][j] == a[i + k - 1][j + k - 1] && pre[i + 1][j] && pre[i][j + 1]) now[i][j] = 1, flag = 1; else now[i][j] = 0; } } if (flag) res = k; else break; for (int i = 0; i <= n - k; i++) for (int j = 0; j <= n - k; j++) pre[i][j] = now[i][j]; } cout << res << endl; } return 0; }