Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

站在前人的肩膀上.......

Posted by StepByStepCnmLife at 2015-05-08 12:32:30 on Problem 3974
#include <stdio.h >
#include <string.h>
#include <stdlib.h>
#define MAXLEN 1000000
#define min(a,b)  (a)<(b)?a:b
char src[MAXLEN];
char dst[MAXLEN*2+1];
int  p[MAXLEN*2+1];
void init(char *src,char *dst)
{
    int i,len,j;
    dst[0]='#';
    len=strlen(src);
    for(i=0,j=0;i<len;i++)
    {
        dst[++j]=src[i];
        dst[++j]='#';
    }
    dst[j+1]='\0';
}
int manacher(char *dst)
{
    int id,mx,i,j,len,max;

    len=strlen(dst);
    for(i=1,id=0,mx=0;i<(len);i++)//计算每一位的p[]值
    {
        if(i<mx)
             p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while((i-p[i])>=0&&(i+p[i])<=(len-1)&&dst[i-p[i]]==dst[i+p[i]])p[i]++;
        if((i+p[i])>mx)
        {
            id=i;
            mx=i+p[i];
        }
       // printf("p[%d]=%d\n",i,p[i]);
    }
    for(i=1,max=0;i<len;i++)
        max=max>p[i]?max:p[i];
    return (max-1);
}
int main()
{
    int cases=1;
    while(scanf("%s",src)!=EOF && strcmp(src,"END"))
    {
        init(src,dst);
        printf("Case %d: %d\n",cases,manacher(dst));
        cases++;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator