Q - Phalanx HDU 2859 ( dp )

Q - Phalanx

Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 2859

Description

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

搜索每一个位置作为对角线的情况下能够到达的最大值

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
int dp[maxn][maxn];
char s[maxn][maxn];
int N,ans;
void work(int x,int y)//枚举每一个点的上方与右边能够到的最大值
{
    if(x==0||y==N-1){
        dp[x][y]=1;
        return ;
    }
    int i=x,j=y;
    while(i>=0&&j<N&&s[i][y]==s[x][j]){
        i--;j++;
    }
    int len=x-i;
    if(len>=dp[x-1][y+1]+1)dp[x][y]=dp[x-1][y+1]+1;
    else dp[x][y]=len;
    ans=max(ans,dp[x][y]);
}
int main()
{
    while(scanf("%d",&N)&&N){
        for(int i=0;i<N;i++){
            scanf("%s",s[i]);
        }
        ans=1;
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++){
            work(i,j);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-15 01:11:54

Q - Phalanx HDU 2859 ( dp )的相关文章

[2016-03-29][HDU][2859][Phalanx]

时间:2016-03-29 15:53:01 星期二 题目编号:[2016-03-29][HDU][2859][Phalanx] 分析:dp[i][j]表示以 (i,j)为左下角 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1000 + 10; char a[maxn][maxn]; int dp[maxn][maxn],ans,n; void func(int x,int

HDU - 2859 Phalanx

题意:求/直线的对称矩阵最大多大 思路:DP 每个点就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1200; int dp[MAXN][MAXN]; char str[MAXN][MAXN]; int n; int main() { while (scanf(&quo

HDU 5928 DP 凸包graham

给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也就是 dp[j][k]代表当前链末端为j,其内部点包括边界数量为k的最小长度.这样最后得到的一定是最优的凸包. 然后就是要注意要dp[j][k]的值不能超过L,每跑一次凸包,求个最大的点数量就好了. 和DP结合的计算几何题,主要考虑DP怎么搞 /** @Date : 2017-09-27 17:27

HDU 4832(DP+计数问题)

HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行,然后相乘累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long ll; const ll MOD = 9999991; const int N = 1005; int t, n, m, k, x, y; ll dp1

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this

HDU 1160 dp中的路径问题

Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, b

hdu 1025 dp 最长上升子序列

1 //Accepted 4372 KB 140 ms 2 //dp 最长上升子序列 nlogn 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 500005; 8 int dp[imax_n]; 9 int d[imax_n]; 10 int a[imax_n]; 11 int n; 12 int len

HDU 4901 DP背包

给你n个数,问你将数分成两个数组,S,T ,T 中所有元素的需要都比S任意一个大,问你S中所有元素进行 XOR 操作和 T 中所有元素进行 &操作值相等的情况有多少种. DP背包思路 dpa[i][j][0]  表示从左开始到i,不取i,状态为j的方案数 dpa[i][j][1]  表示从作开始到i,取i,状态为j的方案数 dpb[i][j]      表示从右开始到i,状态为j的方案数 因为S集合一定在T集合的左边,那么可以枚举集合的分割线,并且枚举出的方案要保证没有重复,如果要保证不重复,只