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

Girls' research

时间:2014-08-09 15:40:19      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

hdu3294:http://acm.hdu.edu.cn/showproblem.php?pid=3294

题意:就是给你一个串,然后求一个最长的回文串,输出起点及串,但是这里在之前要转化一下。

题解:转化一下,就是简单的Manacher算法。

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=2e6+440;
 7 char str1[N],str[N<<1];
 8 int rad[N];
 9 char temp;
10 void Manacher(int *rad,char *str,int n){
11    int i,mx=0,id;
12    for(int i=1;i<n;i++){
13     if(mx>i){
14     rad[i]=min(rad[2*id-i],mx-i);
15    }
16    else
17     rad[i]=1;
18     for(;str[i-rad[i]]==str[i+rad[i]];rad[i]++){
19         if(rad[i]+i>mx){
20             mx=rad[i]+i;
21             id=i;
22         }
23      }
24    }
25 }
26 
27 int main(){
28   while(~scanf("%c %s",&temp,str1)){
29        getchar();
30       int nn=strlen(str1);
31         int n=2*nn+2;
32         str[0]=$;
33       for(int i=0;i<=nn;i++){
34           str[2*i+1]=#;
35           str[2*i+2]=(str1[i]-temp+26)%26+a;
36       }
37       memset(rad,0,sizeof(rad));
38       Manacher(rad,str,n);
39       int ans=0,pos=0;
40       for(int i=0;i<=n;i++){
41         if(rad[i]>=3&&rad[i]>ans){
42             ans=rad[i];
43             pos=i;
44         }
45       }
46        ans--;
47       if(ans==-1)printf("No solution\n");
48       else{
49             int s=pos-ans+1,e=pos+ans-1;
50              printf("%d %d\n",s/2-1,e/2-1);
51         for(int i=pos-ans+1;i<=pos+ans-1;i++){
52             if(str[i]==#)continue;
53             printf("%c",str[i]);
54         }
55         puts("");
56       }
57   }
58 }
View Code

 

Girls' research,布布扣,bubuko.com

Girls' research

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/chujian123/p/3900950.html

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