首页 > 代码库 > hdu 3294 manacher算法

hdu 3294 manacher算法

没什么还说的  也就是这一类的裸题吧


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

char str[200010],str1[400010];
int mark[400010];
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    char str2[5];
    int i,j;
    while(~scanf("%s%s",str2,str))
    {
        int len=strlen(str);
        str1[0]=‘$‘;
        j=0;
        for(i=0;i<len;i++)
        {
            str1[++j]=‘#‘;
            if(str[i]>=str2[0])
                str[i]=str[i]-str2[0]+‘a‘;
            else str[i]=26-(str2[0]-str[i])+‘a‘;
            str1[++j]=str[i];
        }
        str1[++j]=‘#‘;
        memset(mark,0,sizeof(mark));
        int right=0,k=0,id,pp;
        for(i=0;i<=j;i++)
        {
            if(right<=i) mark[i]=1;
            else mark[i]=min(mark[2*id-i],right-i);
            while(str1[i+mark[i]]==str1[i-mark[i]])
            mark[i]++;
            if(right<i+mark[i])
            {
                right=i+mark[i];
                id=i;
            }
            if(mark[i]>k)
            {
                k=mark[i];
                pp=i;
            }
        }
        k-=1;
        if(k<=1) printf("No solution!\n");
        else
        {   
            if(str1[pp]!=‘#‘)printf("%d %d\n",pp/2-(k-1)/2-1,pp/2+(k-1)/2-1);
            else printf("%d %d\n",(pp-1)/2-(k-1)/2-1,(pp+1)/2+(k-1)/2-1);
            for(i=pp-k+1;i<pp+k;i+=2)
            {
                printf("%c",str1[i]);
            }
            printf("\n");
        }
    }
    return 0;
}

hdu 3294 manacher算法