最长回文子串

#include <stdio.h>   
#include <string.h>   
#define MAX 10000   
int min(int a,int b)  
{  
    return a>b?b:a;  
}  
void rebuild(char *s,char *src)  
{  
    int i;  
    s[0]='$',s[1]='#';  
    for(i=0; i<strlen(src); ++i)  
    {  
        s[2*i+2]=src[i];  
        s[2*i+3]='#';  
    }  
    s[2*i+2]='0';  
}  
int longestSubStrNum(char *s)  
{  
    int p[2*MAX+2]= {1,1},id=1,mx=2,i,maxlen=0;  
    for (i = 2; s[i] != '\0'; i++)  
    {  
        p[i] = mx > i ? min(p[2*id-i], mx-i) : 1;  
        while (s[i + p[i]] == s[i - p[i]]) p[i]++;  
        if (i + p[i] > mx)  
        {  
            mx = i + p[i];  
            id = i;  
        }  
        if(maxlen<p[i]-1)maxlen=p[i]-1;  
    }  
    return maxlen;  
}  
int main()  
{  
  
    char src[MAX],s[2*MAX+2];  
    scanf("%s",&src);  
    rebuild(s,src);  
    printf("最长回文串的长度为:%d\n",longestSubStrNum(s));  
    return 0;  
}  

 

posted @ 2013-10-06 00:08  l851654152  阅读(261)  评论(0编辑  收藏  举报