hdu3294 Girls' research manacher

本文介绍了一个关于寻找字符串中最长回文子串的问题,并通过Manacher算法解决该问题。首先对输入字符串进行特定的字符映射操作,然后使用Manacher算法找到变换后字符串中的最长回文子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.

题意:有一个字符串,先将它进行循环映射,再求其中最长的回文子串,长度至少大于等于2

manacher裸题

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=2e5+5;
 7 char s[maxn],t[maxn<<1];
 8 char res[maxn];
 9 int p[maxn<<1];
10 
11 void manacher(){
12     int len=strlen(s),l=0;
13     t[l++]='$';
14     t[l++]='#';
15     for(int i=0;i<len;++i){
16         t[l++]=s[i];
17         t[l++]='#';
18     }
19     t[l]=0;
20     int maxx=0,num=0;
21     for(int i=0;i<l;++i){
22         p[i]=maxx>i?min(p[2*num-i],maxx-i):1;
23         while(t[i+p[i]]==t[i-p[i]])p[i]++;
24         if(i+p[i]>maxx){
25             maxx=i+p[i];
26             num=i;
27         }
28     }
29 }
30 
31 int main(){
32     char c[2];
33     while(scanf("%s%s",c,s)!=EOF){
34         int l=strlen(s);
35         for(int i=0;i<l;++i){
36             s[i]='a'+(s[i]-c[0]+26)%26;
37         }
38         manacher();
39         int pos=0,ans=0;
40         for(int i=1;i<2*l+2;++i){
41             if(p[i]-1>ans){
42                 pos=i;ans=p[i]-1;
43             }
44         }
45         int ans1=0x3f3f3f3f,ans2=0,cnt=0;
46         for(int i=pos-p[pos]+1;i<=pos+p[pos]-1;++i){
47             if(t[i]!='#'){
48                 res[cnt++]=t[i];
49                 if(i<ans1)ans1=i;
50                 if(i>ans2)ans2=i;
51             }
52         }
53         res[cnt]=0;
54         if(p[pos]-1>1){
55             printf("%d %d\n",(ans1-2)/2,(ans2-2)/2);
56             printf("%s\n",res);
57         }
58         else printf("No solution!\n");
59     }
60     return 0;
61 }
View Code

 

转载于:https://www.cnblogs.com/cenariusxz/p/6592543.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值