HDU 2859 Phalanx
2015-10-29 00:57
399 查看
Phalanx
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 776 Accepted Submission(s): 359
[align=left]Problem Description[/align]
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
[align=left]Input[/align]
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.
[align=left]Output[/align]
Each test case
output(输出) one line, the size of the maximum symmetrical sub- matrix.
[align=left]Sample Input[/align]
3 abx cyb zca 4 zaba cbab abbc cacq 0
[align=left]Sample Output[/align]
3 3
这个题目是找最大的均匀矩阵。这个均匀矩阵的特点是由右上角到左下角确定的对角线两边对称。
一开始傻了,按着左上角找转移方程,但是其实从右上角开始找才是最明智的。
dp[i][j]表示以当前点为左下角的矩阵最大的规模,首先,第一行和最后一列的所有元素一定都是1,我们从第2行开始遍历,我们如果仔细观察会发现对应(i, j)这个点的特殊点在(i - 1, j + 1),也就是它的右上的那个点,如果当前的点上面的点依次与当前的点右面的点在以特殊点确定的最大矩阵规模的范围内相同的话,那么我们就可以得出当前点的值。
举个例子
a | b | c |
a | d | b |
g | t | a |
相反如下图:
a | b | c |
t | d | b |
g | t | a |
另外注意当1 * 1的矩阵的规模是1.
代码如下:
/************************************************************************* > File Name: Phalanx.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: Wed 28 Oct 2015 07:40:07 PM CST ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int dp[1010][1010]; char a[1010][1010]; int N; int main(void){ while(1){ scanf("%d", &N); if(N == 0) break; getchar(); int Max = 0; for(int i = 0; i < N; i ++){ for(int j = 0; j < N; j ++){ scanf("%c", &a[i][j]); } getchar(); } if(N == 1){ printf("1\n"); continue; } for(int i = 0; i < N; i ++){ dp[0][i] = 1; dp[i][N - 1] = 1; } for(int i = 1; i < N; i ++){ for(int j = 0; j < N - 1; j ++){ int temp; int flag = 0; for(temp = 1; temp <= dp[i - 1][j + 1]; temp ++){ if(a[i - temp][j] != a[i][j + temp]){ flag = 1; break; } } if(flag) dp[i][j] = temp; else dp[i][j] = dp[i - 1][j + 1] + 1; if(Max < dp[i][j]) Max = dp[i][j]; } } //for(int i = 0 ; i < N; i ++){ // for(int j = 0; j < N; j ++) // cout << dp[i][j]; // cout << endl; //} printf("%d\n", Max); } }
相关文章推荐
- 给信息安全专业学生的书单
- spark学习系列
- OC内存管理一
- Python Django安装
- django sqlite3迁移到mysql
- Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
- 数据库中表散列
- OC基础第五章:构造方法
- 【Java】获取泛型的类型
- android 群英传读书笔记---listview使用技巧(一)
- 怎样删除虚拟机快照
- Java记录 -47- 线性数据结构
- 1.为什么需要用到模板引擎
- loading Bar
- JS模板引擎 :ArtTemplate
- ListActivity 的使用和在 ListView中添加图片文字
- sql按照首字符分组
- Java NIO入门
- iOS输入框UITextView随键盘UIkeyboard弹出与消失改变Rect
- 如何实现服务器高并发