zoukankan      html  css  js  c++  java
  • HDU 2859 Phalanx(二维DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859

    题目大意:对称矩阵是这样的矩阵,它由“左下到右”线对称。 相应位置的元素应该相同。 例如,这里是3 * 3对称矩阵:

         cbx
         cpb
         zcc

    给出任意的n*n的矩阵找出里面最大的对称的子矩阵,输出大小。

    解题思路:有这样一个规律,对于每个字符看该列以上和该行右侧的字符匹配量,如果匹配量大于右上角记录下来的矩阵大小,就是右上角的数值+1,否则就是这个匹配量。根据这个规律,把n*n的点都遍历以便一,直推下去找出最大值就可以了。

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int N=1e3+5;
     6 char map[N][N];
     7 int dp[N][N];
     8 
     9 int main(){
    10     int n;
    11     while(scanf("%d",&n)&&n){
    12         memset(dp,0,sizeof(dp));
    13         for(int i=1;i<=n;i++){
    14             getchar();
    15             for(int j=1;j<=n;j++){
    16                 scanf("%c",&map[i][j]);
    17             }
    18         }
    19         int ans=1;
    20         for(int i=1;i<=n;i++){
    21             for(int j=1;j<=n;j++){
    22                 if(i==1||j==n){
    23                     dp[i][j]=1;
    24                     continue;
    25                 }
    26                 int t1=i,t2=j,cnt=0;
    27                 while(t1>=1&&t2<=n&&map[t1][j]==map[i][t2]){
    28                     t1--;
    29                     t2++;
    30                     cnt++;
    31                 }
    32                 if(cnt>=dp[i-1][j+1]+1)
    33                     dp[i][j]=dp[i-1][j+1]+1;
    34                 else
    35                     dp[i][j]=cnt;    
    36                 ans=max(ans,dp[i][j]);
    37             }
    38         }
    39         printf("%d
    ",ans);
    40     }
    41 }
  • 相关阅读:
    A promise tomorrow is worth a lot less than trying today.
    时间相关函数
    JAVA中日期格式转换各个字母代表含义
    参考链接
    修饰器
    最新提案
    ArrayBuffer
    读懂 ECMAScript 规格
    使用markdown写博客
    linux系统界面转换
  • 原文地址:https://www.cnblogs.com/fu3638/p/7506187.html
Copyright © 2011-2022 走看看