动规-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;
}

--(完)--

转载于:https://www.cnblogs.com/Bcai0797/p/6985657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值