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

--(完)--

时间: 2025-01-06 00:24:59

动规-HDU-2859的相关文章

[ACM] hdu 1231 最大连续子序列 (动规复习)

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17687    Accepted Submission(s): 7828 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j

HDU 1160 FatMouse&#39;s Speed (动规+最长递减子序列)

FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9174    Accepted Submission(s): 4061 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster

HDU 1260 Tickets (动规)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 924    Accepted Submission(s): 468 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

HDU 2844 Coins (动规)

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6904    Accepted Submission(s): 2809 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

HDU 2571 命运 (动规)

命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 3289 Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个机关.要知

HDU 1069 Monkey and Banana (动规)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7248    Accepted Submission(s): 3730 Problem Description A group of researchers are designing an experiment to test the IQ of a

HDU 2845 Beans (动规)

Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2596    Accepted Submission(s): 1279 Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled w

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1171 Big Event in HDU (动规)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22623    Accepted Submission(s): 7948 Problem Description Nowadays, we all know that Computer College is the biggest department

!HDU 1176--DP--(矩阵动规)

题意:有一个数轴,从0到10,小明开始在5这个位置.现在天上开始掉馅饼,小明每次只能移动单位一的长度,求小明最多能接到多少馅饼. 分析:刚开始接触动态规划,还没有真正理解动规的思维,所以刚开始的dp做法不知道对不对但是TLE了.正确的方法是建立一个以时间为行位置为列的矩阵,最初map[i][j]代表的是第i时刻j位置掉的馅饼的数量,状态转移方程:map[i][j]=map[i][j]+max(map[i+1][j-1],map[i+1][j],map[i+1][j+1]).也就是从最底层开始往上