HDU 2859—Phalanx(DP)
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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 大意: 给定一个矩阵,求最大对称矩阵 思路: 一开始怎么想也觉得不会有状态转移的情况,即使有也会复杂度过高 后来看了题解才想通,其实和我一开始的想的有些相似,因为给的时间比较长,n^3的复杂度也会死可以接受的 试想一个n阶的对称阵如何变成n+1的对称阵? 在它的两个底边追加对称的元素即可,对角线元素添加任意元素即可 于是,我们从一个点向他的上与右边推进,直至不匹配 将匹配的个数与dp[i-1][j+1]比较, 如果匹配量大于右上角记录下来的矩阵大小,就是右上角的数值+1,否则就是这个匹配量。 代码:#include<bits/stdc++.h>
using namespace std;
const int MAXN=1300;
char m[MAXN][MAXN];
int dp[MAXN][MAXN];
int main()
{
//freopen("data.in","r",stdin);
int n;
while(~scanf("%d",&n)&&n){
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%c",&m[i][j]);
}
getchar();
}
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// cout<<m[i][j];
// }
// }
// cout<<endl;
memset(dp,0,sizeof(dp));
int res=1;
for(int i=0;i<n;i++){
for(int j=n-1;j>=0;j--){
//cout<<i<<"\t"<<j<<"\t";
//cout<<1111111<<endl;
if(i==0||j==n-1){
dp[i][j]=1;
//cout<<"is "<<dp[i][j]<<endl;
continue;
}
int x=i,y=j;
while(x>=0&&y<=n-1&&m[x][j]==m[i][y]){
x--;
y++;
}
y=y-j;
if(y>dp[i-1][j+1]){
dp[i][j]=dp[i-1][j+1]+1;
}
else{
dp[i][j]=y;
}
//cout<<"is "<<dp[i][j]<<endl;
res=max(res,dp[i][j]);
}
}
printf("%d\n",res);
}
}
HDU 2859—Phalanx(DP)的更多相关文章
- hdu(2859)——Phalanx(dp)
题意: 如今有一个n*n的矩阵,然后每一个格子中都有一个字母(大写或小写组成).然后询问你如今最大的对称子矩阵的边长是多少.注意这里的对角线是从左下角到右上角上去的. 思路: 这道题我自己写出了dp的 ...
- HDU 2859 Phalanx (DP)
Phalanx Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2859 Phalanx ——(DP)
感觉是个n^3的dp,只是可能上界比较松吧..转移见代码.值得注意的一个地方是如果n是1,那么在for里面是不会更新答案的,因此ans要初始化为1. 代码如下: #include <stdio. ...
- HDU 2859 Phalanx(对称矩阵 经典dp样例)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2859 Phalanx Time Limit: 10000/5000 MS (Java/Others) ...
- HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)
Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...
- HDU 3008 Warcraft(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3008 题目大意:人有100血和100魔法,每秒增加 t 魔法(不能超过100).n个技能,每个技能消耗 ...
- hdu 2059 龟兔赛跑(dp)
龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...
- HDU 4832 Chess (DP)
Chess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 4945 2048(dp)
题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 ...
随机推荐
- CSP 画图(201512-3)
问题描述 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术.例如,下图是用 ASCII 字符画出来的 CSPRO 字样. ..____.____..____. ...
- 使用Python的文本挖掘的特征选择/提取
在文本挖掘与文本分类的有关问题中,文本最初始的数据是将文档表示成向量空间模型的一个矩阵,而这个矩阵所拥有的就是不同的词,常采用特征选择方法.原因是文本的特征一般都是单词(term),具有语义信息,使用 ...
- vue-router和webpack懒加载,页面性能优化篇
在vue单页应用中,当项目不断完善丰富时,即使使用webpack打包,文件依然是非常大的,影响页面的加载.如果我们能把不同路由对应的组件分割成不同的代码块,当路由被访问时才加载对应的组件(也就是按需加 ...
- LCT做题笔记
最近几天打算认真复习LCT,毕竟以前只会板子.正好也可以学点新的用法,这里就用来写做题笔记吧.这个分类比较混乱,主要看感觉,不一定对: 维护森林的LCT 就是最普通,最一般那种的LCT啦.这类题目往往 ...
- Collection<E>接口
https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html public interface Collection<E ...
- WPF文本输入框关闭Win10输入法设置
今天一个同事突然站起来给了我一个眼神,我就知道没啥好事.果然给我指出了一些bug,其中一个是这样的,密码输入框只能输入一个字符,再输入就输入不了. 这种降档次的错误咱也能出,果断夺过键盘,疯狂输入一番 ...
- Zookeeper报错Will not attempt to authenticate using SASL解决办法
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq642159746/article/ ...
- Linux shellcode sample
Linux shellcode sample HelloWorld.nasm ;HelloWorld.asm ;Author: Kul Subedi global _start section .te ...
- 客户端相关知识学习(五)之什么是webView
webview是什么?作用是什么?和浏览器有什么关系? Android系统中内置了一款高性能 webkit 内核浏览器,在 SDK 中封装为一个叫做 WebView 组件也就是说WebView是一个基 ...
- [转] JAVA分为三个体系,JavaSE,JavaEE,JavaME(J2ME)的区别以及各个版
Java SE(JavaPlatform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程 ...