传送门:

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

Problem 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
 
Source
 
题目意思:
给你一个字符矩阵,比如n*n的矩阵,问你最大的对称矩阵的阶数是多少
阶数:比如3*3的矩阵,阶数就是3
对称矩阵的定义:副对角线两边的字符对称,副对角线:从左下到右上
 
分析:
我们从大矩阵的右上角看,每次矩阵阶数加一,都是原来矩阵的下面加一行,左边加一列
给一个2阶的对称矩阵,它对称的字符肯定只有一对
给你一个三阶的对称矩阵,它的最下面的行和最左边的列,对称字符的个数肯定是两对
同理,给一个四阶的对称矩阵,它的最下面的行和最左边的列,对称字符的个数肯定是三对
所以如果该矩阵是对称矩阵的话,随着它阶数的加1,它最下面的行和最左的列的对称字符数也加一
这是后面能dp的原因
 
dp【i】【j】:以i,j为矩阵的左下角,最大对称矩阵的阶数
 
如果当前矩阵i,j的最下一行和最左一列的字符数大于矩阵i-1,j+1的最下一行和最左一列的字符数的话,那么当前矩阵就有可能的对称矩阵(有可能)
所以: dp[i][j]=dp[i-1][j+1]+1;
 
记住:我们是从右上角开始推的,往左下角移动
code:
#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样例)的更多相关文章

  1. HDU 2859 Phalanx(二维DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 题目大意:对称矩阵是这样的矩阵,它由“左下到右”线对称. 相应位置的元素应该相同. 例如,这里是 ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. HDU 2859 Phalanx (dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 给你一个n*n的矩阵,问你最大的对称度是多少(左下右上为对称线) dp[i][j]表示i行j列元 ...

  4. HDU 2859—Phalanx(DP)

    Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Today i ...

  5. HDU 2859 Phalanx (DP)

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

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

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

  7. HDU 2859 Phalanx ——(DP)

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

  8. hdu 2859 Phalanx (最大对称子矩阵)

    Problem Description Today is army day, but the servicemen are busy with the phalanx for the celebrat ...

  9. hdu 2859 (二维dp)

    点击打开链接 题意: 给你一个n*n的矩阵,矩阵中只含有26个小写字母,求其中最大的对称矩阵的大小 当我们算到s[i][j]时,每次我们只需要将它上方的和右方的依次比较,看是否相同 注意这里不能只比较 ...

随机推荐

  1. WCF的入门教程dome(一)

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  2. 深入理解JavaScript系列(3):全面解析Module模式

    简介 Module模式是JavaScript编程中一个非常通用的模式,一般情况下,大家都知道基本用法,本文尝试着给大家更多该模式的高级使用方式. 首先我们来看看Module模式的基本特征: 模块化,可 ...

  3. Git代码merge

    Git代码合并遇到如下问题: <<<<<<< HEAD     client.post(url, secretKey, function (data, res ...

  4. Sql server 操作笔记

    (1)更改字段类型 (2)添加字段 alter table class add InKinDate intEXECUTE sp_addextendedproperty N'MS_Description ...

  5. java字节码速查笔记

    java字节码速查笔记  发表于 2018-01-27 |  阅读次数: 0 |  字数统计: |  阅读时长 ≍ 执行原理 java文件到通过编译器编译成java字节码文件(也就是.class文件) ...

  6. Scala 知识点掌握2

    Scala 基础知识点巩固2 1.集合中常用的函数 sum / max / min # 定义一个List[Int]val list1 = List(1,3,4,6,8,9)# 取集合中所有元素的和li ...

  7. 面试基础(二)-mem函数

    常考的函数有下面三个,memset,memcpy,memmove,一定要记住三个函数的函数原型,熟记返回值类型和参数类型,当然也不能忘记参数检查   memset #include<iostre ...

  8. iDempiere 使用指南 使用MRP进行生产及采购排程

    Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...

  9. 企业Web应用创新实验

    我现在设计一个小而美的管理工具,为此费劲心思搞“创新”.“创新”一词已经被滥用,但我...真的想弄出一点创新. 基于Web的企业应用,如CRM.项目管理.OA等软件,尽管经历十几年发展,所谓的理论有所 ...

  10. wxpython CustomTreeCtrl

    转自 http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.customtreectrl.CustomTreeCtrl.html这个网址中有许多控件 ...