首页 > 其他 > 详细

The Minimum Length - HUST 1010(求最小循环节)

时间:2015-08-14 20:52:35      阅读:203      评论:0      收藏:0      [点我收藏+]
题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度。
 
分析:其实就是求最小循环节.......串的长度 - 最大的匹配。
代码如下。
===========================================================================================================
#include<stdio.h>
#include<string.h>

const int MAXN = 1e6+7;
const int oo = 1e9+7;

char s[MAXN];
int next[MAXN];

void GetNext(int N)
{
    int i=0, j=-1;
    next[0] = -1;

    while(i < N)
    {
        if(j==-1 || s[i]==s[j])
            next[++i] = ++j;
        else
            j = next[j];
    }
}

int main()
{

    while(scanf("%s", s) != EOF)
    {
        int N = strlen(s);

        GetNext(N);

        printf("%d\n", N-next[N]);
    }

    return 0;
}

 

The Minimum Length - HUST 1010(求最小循环节)

原文:http://www.cnblogs.com/liuxin13/p/4730929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!