HDU3068最长回文 题解

题目大意:

  求字符串的最长回文子串的长度。

思路:

  Manacher板题,Hash可能会T。要学习Manacher,可参考https://www.felix021.com/blog/read.php?2040

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define M 350000
 5 using namespace std;
 6 
 7 int r[M];
 8 char s[M],str[M];
 9 
10 int main()
11 {
12     while (scanf("%s",s)!=EOF)
13     {
14         int i,k,l=strlen(s),ans=0,maxr=0;
15         for (str[0]='$',i=l;i>=0;i--) str[i+i+2]=s[i],str[i+i+1]='#';
16         for (i=2;i<=l+l;i++)
17         {
18             r[i]=maxr>i?min(r[k+k-i],maxr-i):1;
19             for (;str[i-r[i]]==str[i+r[i]];r[i]++);
20             if (i+r[i]>maxr) maxr=i+r[i],k=i;
21             ans=max(r[i],ans);
22         }
23         printf("%d\n",ans-1);
24     }
25     return 0;
26 }

 

posted @ 2016-08-03 19:08  HHshy  阅读(180)  评论(0编辑  收藏  举报