最长回文 hdu3068(神代码)

最长回文

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6629    Accepted Submission(s): 2284


Problem Description
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
 
Input
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
 
Output
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
 
Sample Input
aaaa abab
 
Sample Output
4 3
 
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 char a[222000];
 6 int p[222000];
 7 int t;
 8 int fun()
 9 {
10     memset(p,0,sizeof(p));
11     int r=0,c=0,i;
12     int max=0;
13     for(i=1; i<t; i++)
14     {
15         p[i]=r>i?min(r-i,p[2*c-i]):0;
16         while (a[i+1+p[i]]==a[i-1-p[i]])
17             p[i]++;
18         max=max<p[i]?p[i]:max;
19         if (i+p[i]>r)
20         {
21             c=i;
22             r=i+p[i];
23         }
24     }
25     return max;
26 }
27 int main()
28 {
29     char x;
30     while((x=getchar())!=EOF)
31     {
32         t=0;
33         a[t++]='^';
34         while(1)
35         {
36             a[t++]='#';
37             a[t++]=x;
38             x=getchar();
39             if(x=='
')break;
40         }
41         a[t++]='#';
42         cout<<fun()<<endl;
43         x=getchar();
44     }
45 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3597216.html