超强悍的ac代码 PKU2406 百度搜的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str [1000010];
int  next[1000010];
   
int getnext()
{
    int i= 0, j= -1; next[0]= -1;
   
    while( str[i] )
    {
        if( j== -1 || str[i]== str[j] )
        {
            ++i,++j;
            next[i]= j;
        }
        else j= next[j];
    }
   
    int len= strlen(str);
    i= len- j;
   
    if( len% i== 0 ) return len/ i;
    else return 1;
}

int main()
{
    while( gets( str), str[0]!= '.' )
    printf("%d\n", getnext() );
   
    return 0;
}

能看懂吗?哈哈。。。。。。

posted @ 2010-07-20 20:20  李洪雷  阅读(224)  评论(0编辑  收藏  举报