zcmu 1616: 最长回文串

来源:互联网 发布:如何看待中央反贪知乎 编辑:程序博客网 时间:2024/06/16 04:54
Description

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

Input

 第一行输入n

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

Output

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

不要想太多 Water


code:

<span style="font-size:18px;">#include<iostream>#include<algorithm>#include<stdio.h>#include<queue>#include<math.h>#include<string.h>#include<stdlib.h>using namespace std;typedef long long ll;int main(){   // freopen("input.txt","r",stdin);    int t;    char s[100005];    scanf("%d",&t);    while(t--){        scanf("%s",s);        int len=strlen(s),flag=0;        for(int i=len;i>0;i--){  //i为字符串长度;            for(int j=0;j<len-i+1;j++){  //j为起点;                int t=j,k=0,l=0;                char s1[100005],s2[100005];                while(k<i/2){                    s1[k]=s[t];                    s2[l]=s[i+j-1-k]; //j+i-1:起点+字符串长度表示最后一个字符;-k:开头向后移一位,结尾向前移一位;                    k++;                    l++;                    t++;                }                s1[k]='\0';                s2[l]='\0';                if(strcmp(s1,s2)==0){                    printf("%d\n",i);                    flag=1;                    break;                }            }            if(flag)                break;        }    }    return 0;}</span>


0 0
原创粉丝点击