HDU 3294 Girls’ research Manacher

题目中有一个规律的字母替换,输出最长回文(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值