首页 > 代码库 > hdu3294Girls' research(manacher)

hdu3294Girls' research(manacher)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294

题目不难,感觉输出比较麻烦。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=2000010;
 6 char s[maxn<<1];
 7 int r[maxn<<1];
 8 char ans[maxn];
 9 char ch;
10 int main()
11 {
12     while(scanf("%c",&ch)!=EOF)
13     {
14         getchar();
15         scanf("%s",s);
16         getchar();
17         int z=a-ch;
18         int id=0,m=0,mx=0;
19         int len=strlen(s);
20         for(int i=len;i>=0;i--)
21         {
22             s[2*i+2]=s[i];
23             s[2*i+1]=#;
24         }
25         s[0]=*;
26         for(int i=2;i<2*len+1;i++)
27         {
28             if(id+r[id]>i) r[i]=min(r[2*id-i],r[id]+id-i);
29             else r[i]=1;
30             while(s[i-r[i]]==s[i+r[i]]) r[i]++;
31             if(m<r[i]) {m=r[i];mx=i;}
32             if(id+r[id]<i+r[i]) id=i;
33 
34         }
35         m--;
36         int sx=-1,e=-1;
37         if(m<=1) puts("No solution!");
38         else  
39             {
40 
41                 int po=0;
42                 for(int i=mx-m;i<=mx+m;i++)
43                 {
44                     if(s[i]!=#&&s[i]!=*) 
45                     {
46                         if(sx==-1&&e==-1) {sx=i;e=i;}
47                         e=i;
48                         ans[po++]=char(s[i]-a+z+26)%26+a;
49 
50                     }
51                 }
52             ans[po]=\0;
53             printf("%d %d\n%s\n",sx/2==0?0:sx/2-1,e/2-1,ans);
54             }
55     }
56     return 0;
57 }

 

hdu3294Girls' research(manacher)