hdu_3294_Girls' research(Manacher)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3294
题意:给你一个字符和一个字符串,第一个字符表示该字符代表a,然后让你求变换后的最长回文区间并输出(这里的回文长度要大于1)
题解:直接上马拉车,然后记录一下区间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #define min(a,b) (a)>(b)?(b):(a) 4 #define max(a,b) (a)>(b)?(a):(b) 5 const int maxn = 200005;//字符串长度 6 int lb,rb;//左右区间 7 struct Manacher{ 8 char str[maxn<<1]; 9 int p[maxn<<1],len,mx,id,tl,ans,i; 10 int maxlen(char *s){ 11 len=strlen(s),mx=0,id=0,tl=0,str[tl++]='$',str[tl++]='#'; 12 for(i=0;i<len;i++)str[tl++]=s[i],str[tl++]='#'; 13 for(i=2,str[tl]=0,ans=0;i<tl;i++){ 14 p[i]=mx>i?min(p[(id<<1)-i],mx-i):1; 15 while(str[i-p[i]]==str[i+p[i]])p[i]++; 16 if(i+p[i]>mx)mx=i+p[i],id=i; 17 if(ans<p[i])ans=p[i],lb=(i-p[i])/2,rb=(i+p[i])/2-2; 18 } 19 return ans-1; 20 } 21 }M; 22 char s[maxn],tt[2]; 23 int main(){ 24 while(~scanf("%s%s",tt,s)){ 25 for(int i=0,tmp=tt[0]-'a';s[i]!='\0';i++){ 26 s[i]-=tmp; 27 if(s[i]<'a')s[i]=s[i]-'a'+'z'+1; 28 } 29 int r=M.maxlen(s); 30 if(r>1){ 31 printf("%d %d\n",lb,rb); 32 for(int i=lb;i<=rb;i++)printf("%c",s[i]); 33 printf("\n"); 34 }else printf("No solution!\n"); 35 } 36 return 0; 37 }