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
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
- 【DP】 HDU 2859 Phalanx
- hdu-2859 Phalanx 【dp】
- DP-HDU-2859-Phalanx
- hdu 2859(DP)
- HDU 2859 简单DP
- [DP] HDU 2859
- hdu 2859 (二维dp)
- 【HDU】2859 Phalanx (DP)
- Q - Phalanx HDU 2859 ( dp )
- HDU 2859 Phalanx 简单DP
- HDU 2859 Phalanx(DP)
- HDU 2859 Phalanx(DP)
- 简单dp,HDU 2859 Phalanx
- HDU 2859 Phalanx (DP)
- HDU 2859 Phalanx (DP)
- HDU 2859 Phalanx (DP)
- HDU DP
- DP? HDU
- Error 42 error C2784: 'bool std::operator <(const std::_Tree<_Traits>
- Linux启动管理
- 冒泡排序
- 数论基本定理及应用(三)
- 自定义肩章badge
- hdu 2859(DP)
- ubuntu 14.04安装oracle12c
- BaseAdapter
- C#基础(WinForm窗体的单例模式,避免窗体被实例化多次)
- wc滚粗记
- JDK,JRE,JVM区别与联系
- ScrollView不能滚动的问题
- 时间分割线:2016年1月的文章都是从以前chinaunix的博客文章
- xutils 2.x(2.6)中的session获得和cookieStore使用