zoukankan      html  css  js  c++  java
  • SPOJ

    PLSQUARE - Palin Square

    Kids just learnt about palindrome strings yesterday. Today, the teacher today was going to teach them about square shapes.  But kids were still excited about palindrome strings. Therefore, the teacher came up with an idea combining square and palindrome in a game.

    The teacher gave to kids a square board size of n, which is n row(s) * n column(s). Each row is a string of n character(s). Then, the teacher asked them to find the palindrome sub-square, which has maximum size.  A palindrome sub-square is a sub square in board such that characters in each row of the sub square give out a palindrome string, and characters in each column of the sub square also give out a palindrome string. 

    Remember: A palindrome string is a string that has the property of reading the same in either direction, e.g. 'racecar', 'solos'.

    When the teacher comes out from toilet, he was too happy and forgot the sub square in solution… Your task now is to help him to find out the maximum size of the palindrome sub square in the given board.

    Input:

    The first row contains an integer n, which is the size of the board. n<=200.

    Each row in next n rows is a string contains only n lowercase characters, which describes the board.

    Output:

    Only 1 integer which is the maximum size of palindrome sub square in the board.

    Example: 

    Input 1:
    4
    babb
    acaz
    babx
    fdhk
    
    Output 1:
    3
    Input 2:
    1
    a
    Output 2:
    1
     


    题意:给你一个字符矩阵,让你求最大的k,使得k*k这个矩阵各行各列都是回文串。

    题解:O(n^4/2)预处理出任意一段字符串是否是回文串,然后从大到小判断 k 是否可行。

                判断的时候先枚举矩阵左上角,然后先行判断向右长度为k的字符串是否回文,

                其次判断这k列是否每一列长度为k的都回文。

               (通俗讲就是先向右能看到长度为k,然后这k个位置都能向下看到长度为k)

    代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <bitset>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <cmath>
    10 #include <list>
    11 #include <set>
    12 #include <map>
    13 #define rep(i,a,b) for(int (i) = (a);(i) <= (b);++ (i))
    14 #define per(i,a,b) for(int (i) = (a);(i) >= (b);-- (i))
    15 #define mem(a,b) memset((a),(b),sizeof((a)))
    16 #define FIN freopen("in.txt","r",stdin)
    17 #define FOUT freopen("out.txt","w",stdout)
    18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
    19 #define mid ((l+r)>>1)
    20 #define ls (id<<1)
    21 #define rs ((id<<1)|1)
    22 #define N 205
    23 #define INF 0x3f3f3f3f
    24 #define INFF 0x3f3f3f3f3f3f3f
    25 typedef long long ll;
    26 const ll mod = 20071027;
    27 const ll eps = 1e-12;
    28 using namespace std;
    29  
    30 int n;
    31 bool isl[N][N][N],isc[N][N][N];
    32 char c[N][N];
    33 bool judge(int k,int x,int y,int f){
    34     if(x >= y)    return true;
    35     if(f == 0){
    36         if(c[k][x] != c[k][y])    return false;
    37         return judge(k, x+1, y-1, f);
    38     }
    39     else{
    40         if(c[x][k] != c[y][k])    return false;
    41         return judge(k, x+1, y-1, f);
    42     }
    43 }
    44 int main()
    45 {IO;
    46     //FIN;
    47     while(cin >> n){
    48         rep(i, 1, n){
    49             rep(j, 1, n)
    50                 cin >> c[i][j];
    51         }
    52 
    53         rep(k, 1, n){    
    54             rep(i, 1, n){
    55                 rep(j, i, n){
    56                     if(judge(k, i, j, 0))    isl[k][i][j] = true;
    57                 }
    58             }
    59         }
    60         rep(k, 1, n){    
    61             rep(i, 1, n){
    62                 rep(j, i, n){
    63                     if(judge(k, i, j, 1))    isc[k][i][j] = true;
    64                 }
    65             }
    66         }
    67 
    68         int ans = -1;
    69         per(k, n, 1){
    70             rep(i, 1, n){
    71                 rep(j, 1, n){
    72                     int ok = 1;
    73                     if(!isl[i][j][j+k-1])    continue;
    74                     rep(m, 1, k){
    75                         if(!isc[j+m-1][i][i+k-1]){
    76                             ok = 0;
    77                             break;
    78                         }
    79                     }
    80                     if(ok){
    81                         ans = k;
    82                         break;
    83                     }
    84                     //cout << "k: " << k << " ans: " << ans << endl;
    85                 }
    86                 if(ans != -1)    break;
    87             }
    88             if(ans != -1)    break;
    89         }
    90         cout << (ans == -1 ? 1 : ans) << endl;
    91     }
    92     return 0;
    93 }
    View Code


  • 相关阅读:
    regulation
    Java第三方类库
    python整个小服务器
    VsFTP出现500 OOPS: cannot change directory的解决办法
    Got error 28 from storage engine
    linux下ftp操作
    linux安装JDK
    Apache + Tomcat + Linux 集群和均衡负载 (Session 同步复制) 配置实
    怎么样才能使得PL/SQL Developer不显示系统表?
    sudo 用法
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351920.html
Copyright © 2011-2022 走看看