hdu 2859(DP)

来源:互联网 发布:长空栈道 知乎 编辑:程序博客网 时间:2024/05/17 06:54

Phalanx

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem 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
3abxcybzca4zabacbababbccacq0
 

Sample Output
33
 
解题思路:
这道题目要找的是对称子矩阵,但不过是以副对角线为对称轴。。首先还是用DP,假设dp[i][j]为在(i,j)的位置能够得到的最大维矩阵,接下来考虑下它由哪些状态转移过来,很显然的,由于是对称阵,那肯定是方阵了,dp[i][j]肯定只能够从dp[i+1][j-1]推过来,接下来就是考虑能否从(i+1,j-1)得到的结果再增加一维,那就假定a[i][j]在(i,j)点,从i-0,j-n共有多少个对称的。。只要a[i][j] > dp[i+1][j-1]那就说明肯定是可以增加1的。。否则,dp[i][j] = a[i][j],这一点很容易忽略的,我最开始就是忽略了,结果一直WA到死!!!以后分析DP时,一定要把状态多考虑清楚。。。另外a[i][j]可以是预先处理的。。刚开始以为会TLE,结果还是A拉。

AC:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char map[1010][1010];int dp[1010][1010],a[1010][1010];int n;int main(){while(cin>>n && n){memset(map,0,sizeof(map));memset(dp,0,sizeof(dp));memset(a,0,sizeof(a));for(int i = 0; i < n; i++){getchar();scanf("%s",map[i]);}for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)for(int k = 0; k < n; k++){if(i + k >= n || j - k < 0) break;if(map[i+k][j] == map[i][j-k])a[i][j]++;else break;}int ans = 1;for(int i = n-1; i >= 0; i--){for(int j = 0; j < n; j++){dp[i][j] = 1;if(i == n - 1 || j == 0) continue;if(dp[i+1][j-1] < a[i][j]){dp[i][j] = dp[i+1][j-1] + 1;ans = max(ans,dp[i][j]);}else dp[i][j] = a[i][j];}}cout<<ans<<endl;}return 0;}



0 0
原创粉丝点击