Topic links: Click here
Title effect: Given a string, asking if you want to turn it into a palindromic sequence, requires a minimum number of letters is added to the tail after the output of the minimum letter string added
Topic Analysis: ask how much the minimum you need to add letters to the tail, in other words the longest palindrome original string is the number suffix, after the longest palindrome suffix obtained by horse-drawn carriage, in front of the prefix is not involved in the fall this output is the answer
Code:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int N=2e5+100;
char s[N*2];//预处理之后的字符串
char str[N];//原本的字符串
int p[N*2];//最长回文半径
int Manacher()
{
int k=0;
s[k++]='!';
for(int i=0;str[i];i++)
{
s[k++]='#';
s[k++]=str[i];
}
s[k++]='#';
s[k]=0;
int ans=0;
p[0]=1;
int id=0,mmax=0;
for(int i=1;i<k;i++)
{
if(i<mmax)
p[i]=min(mmax-i,p[2*id-i]);
else
p[i]=1;
while(s[i-p[i]]==s[i+p[i]])
p[i]++;
if(i+p[i]>mmax)
{
mmax=i+p[i];
id=i;
}
if(i+p[i]==k)//注意这里的判断条件
ans=max(ans,p[i]-1);
}
return ans;
}
int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);
while(scanf("%s",str)!=EOF)
{
int length=Manacher();
printf("%s",str);
for(int i=strlen(str)-length-1;i>=0;i--)
putchar(str[i]);
putchar('\n');
}
return 0;
}