码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2406 Power Srings (kmp循环节) (经典)

时间:2018-08-06 00:43:30      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:color   %s   循环   htm   题目   const   can   class   style   

<题目链接>

题目大意:

给出一个字符串,求其字串在该字符串中循环的最大周期。

解题分析:

length=len-Next[len],len为该字符串的最小循环节,如果len%length==0,那么周期就为len/lenght,如果不能整除,则说明该字符串的字串不具有周期性,输出1。

 KMP最小循环节的证明 >>>

#include <cstdio>
#include <cstring>

const int maxn = 1000000 + 100;

char str[maxn];
int Next[maxn];

void get_next()
{
    int j = 0, k = -1;
    Next[0] = -1;
    while (str[j])
    {
        if (k == -1 || str[j] == str[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}

int main()
{
    while (scanf("%s", &str) != EOF)
    {
        if (str[0] == .)break;
        get_next();
        int len = strlen(str);
        int length = len - Next[len];        //最小循环节
        
        if (len%length == 0)
        {
            printf("%d\n", len / length);
        }
        else
            printf("1\n");
    }
    return 0;
}

 

2018-08-05

poj 2406 Power Srings (kmp循环节) (经典)

标签:color   %s   循环   htm   题目   const   can   class   style   

原文地址:https://www.cnblogs.com/00isok/p/9427886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!