HDU - 2859 Phalanx (dp_最大对称子图)

Today is army day, but the servicemen are busy with the phalanx for the celebration of the 60th anniversary of the PRC.
A phalanx is a matrix of size n*n, each element is a character (a~z or A~Z), standing for the military branch of the servicemen on that position.
For some special requirement it has to find out the size of the max symmetrical sub-array. And with no doubt, the Central Military Committee gave this task to ALPCs.
A symmetrical matrix is such a matrix that it is symmetrical by the “left-down to right-up” line. The element on the corresponding place should be the same. For example, here is a 3*3 symmetrical matrix:
cbx
cpb
zcc

Input

There are several test cases in the input file. Each case starts with an integer n (0<n<=1000), followed by n lines which has n character. There won’t be any blank spaces between characters or the end of line. The input file is ended with a 0.

Output

Each test case output one line, the size of the maximum symmetrical sub- matrix.

Sample Input

3
abx
cyb
zca
4
zaba
cbab
abbc
cacq
0

Sample Output

3
3

题意:

求最大对称子图(关于反对角线对称)

dp[ i ][ j ] 表示以 (i, j) 为左下角的最大对称子图,必须包含该点

每次向上向下查找最大匹配数,如果大于右上角的dp值,那么就是右上角的dp值 + 1,因为如果再多的话内一层会不再匹配。

如果小于右上角的dp值,那么就是该点的最大匹配数,因为如果再多的话外层会不再匹配

左边是第一种情况,右边是第二种情况,红色框表示该点的最大对称子图

                      

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 1e3 + 10;

int dp[N][N];   ///dp[i][j]:以(i, j)为左下角的最大对称矩阵,必须包括(i, j)
char s[N][N];

int main()
{
    int n;
    while(~scanf("%d", &n) && n)
    {
        for(int i = 1; i <= n; ++i)
        {
            scanf("%s", s[i] + 1);
        }
        int maxx = 0;
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= n; ++j)
            {
                if(i == 1 || j == n)    ///每个字母都算一个
                {
                    dp[i][j] = 1;
                }
                int x = i;
                int y = j;
                while(x >= 1 && y <= n && s[x][j] == s[i][y])   ///从(i, j)向上向右找最大匹配的数量
                {
                    x--;
                    y++;
                }
                int cnt = i - x;    ///最大匹配数量
                if(cnt > dp[i - 1][j + 1])              ///超过了以(i - 1, j + 1)为左下角的最大对称矩阵
                {
                    dp[i][j] = dp[i - 1][j + 1] + 1;    ///最多也就延伸到dp[i - 1][j + 1] + 1;(向左下方扩展了一行)
                }
                else
                {
                    dp[i][j] = cnt;
                }
                maxx = max(maxx, dp[i][j]);
            }
        }
        cout<<maxx<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

02-16 01:39
南昌大学 Java
重剑Ds:感觉不太可能 后端都减飞了 根本不缺人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8665次浏览 80人参与
# 你的实习产出是真实的还是包装的? #
1597次浏览 40人参与
# MiniMax求职进展汇总 #
23662次浏览 305人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7337次浏览 40人参与
# 重来一次,我还会选择这个专业吗 #
433258次浏览 3926人参与
# 简历第一个项目做什么 #
31475次浏览 324人参与
# 巨人网络春招 #
11285次浏览 223人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186809次浏览 1118人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152237次浏览 887人参与
# 研究所笔面经互助 #
118840次浏览 577人参与
# 简历中的项目经历要怎么写? #
309904次浏览 4183人参与
# 面试紧张时你会有什么表现? #
30466次浏览 188人参与
# 你今年的平均薪资是多少? #
212956次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63247次浏览 793人参与
# 我的求职精神状态 #
447945次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76388次浏览 374人参与
# 高学历就一定能找到好工作吗? #
64275次浏览 620人参与
# 牛客AI文生图 #
21395次浏览 238人参与
# 你怎么看待AI面试 #
179751次浏览 1224人参与
# 正在春招的你,也参与了去年秋招吗? #
363105次浏览 2635人参与
# 腾讯音乐求职进展汇总 #
160539次浏览 1109人参与
# 职能管理面试记录 #
10787次浏览 59人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务