题目中有一个规律的字母替换,输出最长回文(len>=2)的起始点和终点和替换后的回文。
#include<iostream>
#include<queue>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
const int MAX=222222<<1;
char Str[MAX],Ch;
int Len;
int Radius[MAX];
void Manacher()
{
Radius[0]=1;
for (int i=1,k=0;i<Len;i++)
{
if (k+Radius[k]-1<i) Radius[i]=1;
else Radius[i]=min(Radius[2*k-i],k+Radius[k]-i);
while (Str[i-Radius[i]]==Str[i+Radius[i]]) Radius[i]++;
if (k+Radius[k]-1<i+Radius[i]-1) k=i;
}
}
int main()
{
cin.sync_with_stdio(false);
while (cin>>Ch>>Str)
{
int temp=Ch-'a';
Len=strlen(Str);
for (int i=Len;i>=0;i--)
{
Str[i*2+1]='#';
Str[i*2+2]=(Str[i]-'a'-temp+26)%26+'a';
}
Len=Len*2+2;
Str[0]='*',Str[Len]='\0';
Manacher();
int Max=1,Index=-1;
for (int i=0;i<Len;i++)
if (Radius[i]-1>Max)
Max=Radius[i]-1,Index=i;
if (Index!=-1)
{
int Start=(Index-Max+1-2)/2,End=(Index+Max-1-2)/2;
cout<<Start<<' '<<End<<'\n';
for (int i=Index-Max+1;i<=Index+Max-1;i++)
if (Str[i]!='#')
cout<<Str[i];
cout<<'\n';
}
else
cout<<"No solution!\n";
}
return 0;
}
HDU 3294 Girls’ research Manacher
最新推荐文章于 2020-08-08 15:05:51 发布