HDU 2859 Phalanx DP

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2859

题目大意:求一个矩阵的按反对角线对称的子矩阵的行最大值。

解题思路:dp[i][j]:=以(i,j)为左下角起点的满足条件的矩阵的行最大值,那么将第一行和最后一列初始化为1之后从第0行第n-1列倒着初始化即可。

代码:

 1 const int maxn = 1e3 + 100;
 2 int n;
 3 char maze[maxn][maxn];
 4 int dp[maxn][maxn]; 
 5 
 6 void init(){
 7     memset(dp, 0, sizeof(dp));
 8     for(int i = 0; i < n; i++){
 9         dp[0][i] = 1;
10         dp[i][n - 1] = 1;
11     }
12 }
13 void solve(){
14     int ans = 1;
15     for(int i = 1; i < n; i++){
16         for(int j = n - 2; j >= 0; j--){
17             int tmp = dp[i - 1][j + 1], cnt = 1;
18             for(int k = 1; k <= tmp; k++)
19                 if(maze[i - k][j] == maze[i][j + k]) cnt++;
20                 else break;
21             dp[i][j] = cnt;
22             ans = max(ans, dp[i][j]);
23         }
24     }
25     printf("%d\n", ans);
26 }
27 
28 int main(){
29     while(scanf("%d", &n) && n){
30         for(int i = 0; i < n; i++)
31             scanf("%s", maze[i]);
32         init();
33         solve();
34     }
35 }

题目:

Phalanx

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1988    Accepted Submission(s): 988


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
3 abx cyb zca 4 zaba cbab abbc cacq 0
 

 

Sample Output
3 3
 

 

Source
posted @ 2017-08-17 21:43  EricJeffrey  阅读(183)  评论(0编辑  收藏  举报