美文网首页工作生活
1615:最长回文串

1615:最长回文串

作者: Celia_QAQ | 来源:发表于2019-07-04 20:39 被阅读0次

    Description

    求一个字符串的最长回文串

    Input

    第一行输入n

    接下来n行每行每行一个字符串长度<=100000

    Output

    输出最长回文串的长度
    Sample Input
    3
    abababa
    aaaabaa
    acacdas
    Sample Output
    7
    5
    3
    HINT

    不要想太多 Water

    参考:https://blog.csdn.net/litianxiang_kaola/article/details/52201226
    https://blog.csdn.net/qq_38735931/article/details/81665148

    AC代码:

    #include<vector>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=300050;
    
    int main(){
       int t,temp;
        char s[10000];
       while(~scanf("%d",&t)){
           scanf("%s",s);
           int n=strlen(s);
           bool flag=false;
           for(int i=n;i>0;i--){
            for(int j=0;j<n-i+1;j++){
                int t=j,k=0,l=0;
                char c1[10000],c2[10000];
                while(k<i/2){
                    c1[k]=s[t];
                    c2[l]=s[i+j-1-k];
                    k++,l++,t++;
                }
                c1[k]='\0';
                c2[l]='\0';
                if(strcmp(c1,c2)==0){
                    printf("%d\n",i);
                    flag=true;
                    break;
                }
               }
               if(flag) break;
           }
        
        }
         return 0;
    }
    

    相关文章

      网友评论

        本文标题:1615:最长回文串

        本文链接:https://www.haomeiwen.com/subject/lrrdhctx.html