HDU 3068 Manacher

题目链接:http://hdu.hustoj.com/showproblem.php?pid=3068

今天学习一下马拉车算法,虽然mg讲过,但是没有系统去学。

算法学习:参考博客

马拉车模板题。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 110000+10;
 4 char s[maxn*2];
 5 int p[maxn*2];
 6 int manacher(int n)
 7 {
 8     int id = 0,maxlen = 0;
 9     for(int i=2;i<n;i++)
10     {
11         if(p[id]+id>i) p[i] = min(p[2*id-i],p[id]+id-i);
12         else p[i] = 1;
13         while(s[i-p[i]]==s[i+p[i]]) p[i]++;
14         if(id+p[id]<i+p[i]) id = i;
15         if(maxlen<p[i]) maxlen = p[i];
16     }
17     return (maxlen-1);
18 }
19 int main()
20 {
21     while(scanf("%s",s)!=EOF)
22     {
23         int len = strlen(s);
24         for(int i=len;i>=0;i--)
25         {
26             s[i*2+2] = s[i];
27             s[i*2+1] = '#';
28         }
29         s[0]='*';
30         printf("%d\n",manacher(2*len+1));
31     }
32 }

 

posted @ 2017-10-26 21:31  卷珠帘  阅读(147)  评论(0编辑  收藏  举报