码迷,mamicode.com
首页 > 其他好文 > 详细

poj3947Palindrome(manacher)

时间:2017-03-25 00:57:59      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:i++   ble   lan   logs   clu   题目   cstring   can   std   

题目链接:http://poj.org/problem?id=3974

manacher模板题。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1000010;
 6 char s[maxn<<1];
 7 int r[maxn<<1];
 8 
 9 int main()
10 {
11     int cas=0;
12     while(scanf("%s",s))
13     {
14         if(strcmp(s,"END")==0) return 0;
15         else
16         {
17             int n=strlen(s);
18             for(int i=n;i>=0;i--)
19             {
20                 s[i*2+2]=s[i];
21                 s[i*2+1]=#;
22             }
23             s[0]=*;
24             int id=0,m=0;
25             for(int i=2;i<n*2+1;i++)
26             {
27                 if(id+r[id]>i) r[i]=min(r[id*2-i],r[id]+id-i);
28                 else r[i]=1;
29                 while(s[i-r[i]]==s[i+r[i]]) r[i]++;
30                 if(id+r[id]<i+r[i]) id=i;
31                 if(m<r[id]) m=r[id];
32             }
33             printf("Case %d: %d\n",++cas,--m);
34         }
35     }
36 }

 hdu3068最长回文

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=110010;
 6 char s[maxn<<1];
 7 int r[maxn<<1];
 8 int main()
 9 {
10     while(scanf("%s",s)!=EOF)
11     {
12         int len=strlen(s);
13         for(int i=len;i>=0;i--)
14         {
15             s[2*i+2]=s[i];
16             s[2*i+1]=#;
17         }
18         s[0]=*;
19         int id=0,m=0;
20         for(int i=2;i<len*2+1;i++)
21         {
22             if(r[id]+id>i) r[i]=min(r[2*id-i],r[id]+id-i);
23             else r[i]=1;
24             while(s[i-r[i]]==s[i+r[i]]) r[i]++;
25             if(id+r[id]<i+r[i]) id=i;
26             if(m<r[i]) m=r[i];
27         }
28         printf("%d\n",m-1);
29     }
30 }

 

poj3947Palindrome(manacher)

标签:i++   ble   lan   logs   clu   题目   cstring   can   std   

原文地址:http://www.cnblogs.com/yijiull/p/6614017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!