hihocode ---1032

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 
 5 using namespace std;
 6 
 7 const int MAXN = 1000000 + 10;
 8 char Ma[MAXN*2];
 9 int Mp[MAXN*2];
10 void Manacher(char s[],int len)
11 {
12     int l=0;
13     Ma[l++]='$';
14 
15     Ma[l++]='#';
16     for(int i=0;i<len;i++)
17     {
18         Ma[l++]=s[i];
19         Ma[l++]='#';
20     }
21     Ma[l]=0;
22     int mx=0,id=0;
23     for(int i=0;i<l;i++)
24     {
25         Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
26         while(Ma[i+Mp[i]]==Ma[i-Mp[i]])Mp[i]++;
27         if(i+Mp[i]>mx)
28         {
29             mx=i+Mp[i];
30             id=i;
31         }
32     }
33 }
34 
35 char s[MAXN];
36 int main()
37 {
38     int t;
39     cin >> t;
40     while (t--)
41     {
42         memset(s,0,sizeof(s));
43 
44         scanf("%s",s );
45         {
46             int len=strlen(s);
47             Manacher(s,len);
48             int ans=0;
49             for(int i=0;i<2*len+2;i++)
50             ans=max(ans,Mp[i]-1);
51             printf("%d\n",ans);
52         }
53     }
54 
55     return 0;
56 }
View Code

Manacher 最长回文子串

 

http://hihocoder.com/problemset/problem/1032

posted on 2015-06-09 20:51  yifi  阅读(288)  评论(0编辑  收藏  举报

导航