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)的更多相关文章

  1. hdu(2859)——Phalanx(dp)

    题意: 如今有一个n*n的矩阵,然后每一个格子中都有一个字母(大写或小写组成).然后询问你如今最大的对称子矩阵的边长是多少.注意这里的对角线是从左下角到右上角上去的. 思路: 这道题我自己写出了dp的 ...

  2. HDU 2859 Phalanx (DP)

    Phalanx Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 2859 Phalanx ——(DP)

    感觉是个n^3的dp,只是可能上界比较松吧..转移见代码.值得注意的一个地方是如果n是1,那么在for里面是不会更新答案的,因此ans要初始化为1. 代码如下: #include <stdio. ...

  4. HDU 2859 Phalanx(对称矩阵 经典dp样例)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2859 Phalanx Time Limit: 10000/5000 MS (Java/Others)  ...

  5. 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 ...

  6. HDU 3008 Warcraft(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3008 题目大意:人有100血和100魔法,每秒增加 t 魔法(不能超过100).n个技能,每个技能消耗 ...

  7. hdu 2059 龟兔赛跑(dp)

    龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...

  8. HDU 4832 Chess (DP)

    Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. HDU 4945 2048(dp)

    题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 ...

随机推荐

  1. NOIP2017普及组比赛总结

    期中考总结&NOIP2017总结 2017年11月11日,我第二次参加NOIP普及组复赛.上一年,我的得分是250分,只拿到了二等奖.我便把目标定为拿到一等奖,考到300分以上. 早上8点多, ...

  2. 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

    题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...

  3. C++练习 | 单向链表类模板(包含类模板中静态变量初始化格式)

    #include <iostream> #include <string> using namespace std; template <class T> clas ...

  4. Laravel5.5 实现session配置

    \Illuminate\Session\Middleware\StartSession::class,\Illuminate\View\Middleware\ShareErrorsFromSessio ...

  5. 教你用python爬取网站美女图(附代码及教程)

    我前几篇文章都是说一些python爬虫库的用法,还没有说怎样利用好这些知识玩一些好玩的东西.那我今天带大家玩好玩又刺激的,嘻嘻!对了,requests库和正则表达式很重要的,一定要学会!一定要学会!! ...

  6. CentOS7部署HDP3.1.0.0

    Apache Ambari是一个基于Web的支持Apache Hadoop集群的供应.管理和监控的开源工具,Ambari已支持大多数Hadoop组件,包括HDFS.MapReduce.Hive.Pig ...

  7. js之运算符(逻辑运算符)

    逻辑运算符通常用于布尔型(逻辑)值.这种情况下,它们返回一个布尔值.它经常和关系运算符一起配合使用.“&&” .“!”和“ ||” 运算符会返回一个指定操作数的值,因此,这些运算符也用 ...

  8. Object.keys()返回对象自身可枚举属性组成的数组

    Object.keys()方法是对一个对象的key遍历,会把key组成一个数组返回 示例: // 参数为数组时,返回的是数组的索引 let arr1 = [1, 2, '3'] console.log ...

  9. Redis主从配置以及哨兵模式

    Redis主从模式,应用写master,读slave,减轻master的压力. 配置主结点: daemonize yes port 6379bind 0.0.0.0 pidfile /opt/redi ...

  10. 【LeetCode】451-根据字符出现频率排序

    题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...