HDU 2859 Phalanx(对称矩阵 经典dp样例)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=2859
Phalanx
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2792 Accepted Submission(s): 1357
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
abx
cyb
zca
4
zaba
cbab
abbc
cacq
0
3
#include<bits/stdc++.h>
using namespace std;
#define max_v 1005
char a[max_v][max_v];
int dp[max_v][max_v];
int main()
{
int n;
while(cin>>n,n)
{
memset(dp,,sizeof(dp));
getchar();
for(int i=;i<n;i++)
{
scanf("%s",&a[i]);
dp[][i]=;//初始化
}
int ans=;
for(int i=;i<n;i++)
{
for(int j=n-;j>=;j--)//右上角开始
{
int x=i-,y=j+;//上一个矩阵的左下角坐标
int num=;//对称字符个数
while(x>=&&x<n&&y>=&&y<n&&a[i][y]==a[x][j])//坐标在大矩阵内且字符对称
{
x--;//横坐标上移
y++;//纵坐标右移
num++;//对称字符数加1
}
if(num>dp[i-][j+])//判断当前矩阵是不是对称矩阵
{
dp[i][j]=dp[i-][j+]+;
}else
{
dp[i][j]=num;
}
ans=max(ans,dp[i][j]);//找对称矩阵最大阶
}
}
printf("%d\n",ans);
}
return ;
}
HDU 2859 Phalanx(对称矩阵 经典dp样例)的更多相关文章
- HDU 2859 Phalanx(二维DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 题目大意:对称矩阵是这样的矩阵,它由“左下到右”线对称. 相应位置的元素应该相同. 例如,这里是 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDU 2859 Phalanx (dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 给你一个n*n的矩阵,问你最大的对称度是多少(左下右上为对称线) dp[i][j]表示i行j列元 ...
- HDU 2859—Phalanx(DP)
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Today i ...
- 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*n的矩阵,然后每一个格子中都有一个字母(大写或小写组成).然后询问你如今最大的对称子矩阵的边长是多少.注意这里的对角线是从左下角到右上角上去的. 思路: 这道题我自己写出了dp的 ...
- HDU 2859 Phalanx ——(DP)
感觉是个n^3的dp,只是可能上界比较松吧..转移见代码.值得注意的一个地方是如果n是1,那么在for里面是不会更新答案的,因此ans要初始化为1. 代码如下: #include <stdio. ...
- hdu 2859 Phalanx (最大对称子矩阵)
Problem Description Today is army day, but the servicemen are busy with the phalanx for the celebrat ...
- hdu 2859 (二维dp)
点击打开链接 题意: 给你一个n*n的矩阵,矩阵中只含有26个小写字母,求其中最大的对称矩阵的大小 当我们算到s[i][j]时,每次我们只需要将它上方的和右方的依次比较,看是否相同 注意这里不能只比较 ...
随机推荐
- [Matlab] figure
figure只能设置序号 不能设置title 而stem和plot可以设置title
- Class.forName和ClassLoader的区别
一 看名字就知道了,一个是类的创建,一个类加载器 二 再看下Class.forName源码,调用了ClassLoader @CallerSensitive public static Class< ...
- Jvav Collection-List
package 集合; import java.util.ArrayList; import java.util.Collection; /** * 集合和数组的区别: * 1.长度 * 数组长度固定 ...
- spring mvc 外键允许Null
spring mvc 定义模型外键时允许为Null 新手记录,习惯定义模型时将外键的数据类型为long,基本类型. 这样生成的外键是不允许为Null,但是可以通过设置字段的Column注解,使之允许为 ...
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
使用Git Pull项目的时候出现这个问题: The current branch is not configured for pull No value for key branch.master. ...
- 04.Path类的学习
path 是路径的意思. path类是一个静态类,所以path是一个工具类. Path类是专门用来操作路径的. Path的常用方法: namespace _15.Path类的学习 { class Pr ...
- oracle相关常识
1.数据类型 VARCHAR2() NUMBER() DATE CLOB BLOB 2.复制表:create table tableName as select * from emp3.新增列:ALT ...
- Android仿QQ界面
最近这几天,一直跟着朋友们聚会什么的,没怎么做项目,今天总算是有时间开电脑继续做我的项目了.下面我就把我做的效果展示一下. 这是模仿了qq的界面效果.因为代码比较长就不粘贴代码了.需要的小伙伴可以跟我 ...
- html5 填表 表单 input output 与表单验证
1.<output> Js计算结果 <form oninput="res.value = num1.valueAsNumber*num2.valueAsNumber ...
- Java入门到精通——框架篇之Spring源码分析Spring两大核心类
一.Spring核心类概述. Spring里面有两个最核心的类这是Spring实现最重要的部分. 1.DefaultListableBeanFactory 这个类位于Beans项目下的org.spri ...