动规-HDU-2859

http://acm.hdu.edu.cn/showproblem.php?pid=2859

Phalanx

给定一个n*m的字符矩阵,求最大以副对角线对称的子方阵行数。

解题报告

思路

对于给定字符矩阵M:

记以元素M[i][j]为左下角的最大副对角线对称子方阵行数为dp[i][j]。

假设已经求得dp[i-1][j+1],欲求dp[i][j],则需要以dp[i-1][j+1]为上界,判断从M[i][j]为左下角开始的M[i][k]与M[k][j]有多少个连续相同即可。

那么就可以两层循环遍历每个元素,对于每个元素按上述方法求得dp[i][j],取其中的最大值即为解。

(这种O(n^3)的做法能过也多亏给的时间限制够宽松...)

代码

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

const int maxn = 1003;

string str[maxn];
int dp[maxn][maxn];
int n;
int ans;

int Check(int x, int y) {
    int tx = x, ty = y, len = dp[x-1][y+1];
    int cnt = 0;
    for (int i = 0; i < len; i++) {
        tx--;
        ty++;
        if (tx < 0 || ty >= n) break;
        if (str[tx][y] != str[x][ty]) break;
        cnt++;
    }
    return cnt;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (cin >> n, n) {
        for (int i = 0; i < n; i++) {
            cin >> str[i];
            for (int j = 0; j < n; j++) {
                dp[i][j] = 1;
            }
        }

        ans = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n - 1; j++) {
                dp[i][j] = Check(i, j) + 1;
                ans = max(ans, dp[i][j]);
            }
        }

        cout << ans << endl;
    }

    return 0;
}

--(完)--

posted @ 2017-06-11 17:08  Bcai  阅读(136)  评论(0编辑  收藏  举报