zoukankan      html  css  js  c++  java
  • 【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))

    题意:

    输入一个包含空格的字符串,输出它的最长回文子串的长度。

    AAAAAccepted code:

     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 char s[1007];
     5 int main(){
     6     ios::sync_with_stdio(false);
     7     cin.tie(NULL);
     8     cout.tie(NULL);
     9     cin.getline(s+1,1005);
    10     int ans=0;
    11     int cnt=strlen(s+1);
    12     if(cnt)
    13         ans=1;
    14     for(int i=1;i<=cnt;++i){
    15         int l=i-1,r=i+1;
    16         while(1){
    17             if(l<1||r>cnt)
    18                 break;
    19             if(s[l]==s[r]){
    20                 ans=max(ans,r-l+1);
    21                 --l;
    22                 ++r;
    23             }
    24             else
    25                 break;
    26         }
    27         l=i,r=i+1;
    28         while(1){
    29             if(l<1||r>cnt)
    30                 break;
    31             if(s[l]==s[r]){
    32                 ans=max(ans,r-l+1);
    33                 --l;
    34                 ++r;
    35             }
    36             else
    37                 break;
    38         }
    39     }
    40     cout<<ans;
    41     return 0;
    42 }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    常见设备标记长度查询
    word怎么在方框中打对号
    shell dict 操作
    词表数据转换
    GoLand tool tips
    mac使用技巧
    人生三大陷阱
    【js重学系列】执行上下文
    uniapp-ui库
    【js重学系列】instanceof
  • 原文地址:https://www.cnblogs.com/ldudxy/p/11588579.html
Copyright © 2011-2022 走看看