Fellow me on GitHub

HDU2859(KB12-Q DP)

Phalanx

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


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

 
题意:找给出的方阵中,以左下到右上方向为对称轴的最大对称子方阵。
题解:dp[i][j]表示以ch[i][j]格子为左下角的最大对称子方阵。dp[i][j] = min(dp[i-1][j+1]+1, 从(i,j)点向上、右方向匹配的对称数)
 1 //2017-04-20
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 1010;
10 char ch[N][N];
11 int dp[N][N];
12 
13 int main()
14 {
15     int n;
16     while(scanf("%d", &n)!=EOF && n)
17     {
18         for(int i = 0; i < n; i++)
19             scanf("%s", ch[i]);
20         int ans = 1;
21         for(int i = 0; i < n; i++)
22         {
23             for(int j = n-1; j >= 0; j--)
24             {
25                 if(i == 0 || j == n-1)
26                 {
27                     dp[i][j] = 1;
28                     continue;
29                 }
30                 int u, r, cnt = 1;
31                 for(int k = 1; k <= dp[i-1][j+1]; k++){
32                     u = i-k;
33                     r = j+k;
34                     if(ch[u][j] == ch[i][r])cnt++;
35                     else break;
36                 }
37                 dp[i][j] = min(dp[i-1][j+1]+1, cnt);
38                 if(ans < dp[i][j])ans = dp[i][j];
39             }
40         }
41         printf("%d\n", ans);
42     }
43 
44     return 0;
45 }

 

posted @ 2017-04-20 22:30  Penn000  阅读(257)  评论(0编辑  收藏  举报