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

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

    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
     
    题目大意 给一个方阵,找一个最大子阵,该子阵满足关于从左下到右上的线对称,参照样例
    思路 按问题描述可知,一个满足条件的子阵,其延对角线的子阵也是满足条件的,若以dp[i][j]表示map[i][j]为左下点的最大满足条件的方阵,则只需要比较第i行和第j列就能得到dp[i][j]和dp[i - 1][j + 1]的关系了,思路很简单,需要注意边界情况的判断和初始化,还有ans最好初始化为1
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<string>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 char map[1005][1005];
     9 int dp[1005][1005];
    10 
    11 int main(){
    12     ios::sync_with_stdio( false );
    13 
    14     int n;
    15     while( cin >> n, n ){
    16         for( int i = 0; i < n; i++ )
    17             cin >> map[i];
    18         int ans = 1;
    19 
    20         for( int i = 0; i < n; i++ ){
    21             for( int j = 0; j < n; j++ ){
    22                 if( i == 0 || j == n - 1 ){
    23                     dp[i][j] = 1;
    24                     continue;
    25                 }
    26 
    27                 int x = i;
    28                 int y = j;
    29 
    30                 while( x >= 0 && y < n && map[x][j] == map[i][y] ){
    31                     x--;
    32                     y++;
    33                 }
    34 
    35                 if( i - x > dp[i - 1][j + 1] )
    36                     dp[i][j] = dp[i - 1][j + 1] + 1;
    37                 else dp[i][j] = i - x;
    38 
    39                 ans = max( ans, dp[i][j] );
    40             }
    41         }
    42 
    43         cout << ans << endl;
    44     }
    45 
    46     return 0;
    47 }
  • 相关阅读:
    [原]openstack-kilo--issue(二) openstack auth error
    [原]关闭selinux
    [原]Failed to load SELinux policy. System Freezing ----redhat7or CentOS7 bug
    [转]Understanding OpenStack Authentication: Keystone PKI
    第七节:Core SignalR中的重连机制和心跳监测机制详解
    第八节:SQLServer函数大全(一)--字符串函数、数学函数
    第四节:SQLServer数据类型详解
    第三节:从多个角度分析顺序表和链表区别和特点
    第二节:线性表→顺序表→链表 逐个击破
    第一节:时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5451336.html
Copyright © 2011-2022 走看看