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 ...
随机推荐
- std::tr1::function和bind组件
C++中std::tr1::function和bind 组件的使用 在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数 ...
- 2019中山纪念中学夏令营-Day4[JZOJ]
Begin (题目的排序方式:难易程度) 什么是对拍: 对拍是一种在写完程序后,验证自己程序是不是正解的比较方便的方法. 实现过程: 对同一道题,再打一个暴力程序,然后用一些大数据等跑暴力程序来进行验 ...
- 模块之re模块 正则表达式
正则表达式,正则表达式在处理字符串上有先天的优势,尤其大数量的字符串.先来记一个网站,此网站功能就是关于正则表达式方面的应用http://tool.chinaz.com/regex/ 单纯的正则表达式 ...
- [Next] 三.next自定义服务器和路由
next 服务端渲染 实际上,next 一直都是执行的服务端渲染.npm start执行的是 next 自带的服务器来运行你的应用.next 是支持自定义服务器的,同时能够支持现有的路由和模式,你可以 ...
- ITCAST-C# 委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _12委 ...
- markdown图片转换demo
markdown图片转换demo 一直以来都是用Markdown来写博客的,但是它的图片嵌入实在是太让人头秃,逼得我能找网上的图片就不用自己的,实在是麻烦.所以我在发现了一个可以生成markdown样 ...
- O009、KVM 网络虚拟化基础
参考https://www.cnblogs.com/CloudMan6/p/5289590.html 网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源, ...
- java传值与传引用
一.传值与传引用 1.不管java参数的类型是什么,一律传递参数的副本. 在thinking in java中,明确指出,如果java是传值,那么传递的是值的副本,如果java传递的是引用,那么传递的 ...
- 转载: java获取json数组格式中的值
转自:https://www.cnblogs.com/kkxwze/p/11134846.html 第一种方法: String str = "{'array':[{'id':5,'nam ...
- elasticsearch设置执行脚本并添加开机启动 (转)
elasticsearch设置执行脚本并添加开机启动 在/etc/init.d目录下新建文件elasticsearch #!/bin/sh #chkconfig: 2345 80 05 #descri ...