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

POJ 2406 Power String

时间:2018-06-29 01:18:01      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:str   std   main   poj   names   输出   bubuko   scanf   最小   

技术分享图片

算出next数组.

对于任何一个循环字串,len-next[len]必为最小循环节长度

若len%(len-next[len])==0

即为循环字串,n=len/(len-next[len])

否则输出1

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+10;
char str[N]; 
int next[N],ans,len;
void make()
{
    int i=0,j=-1;
    next[i]=j;
    while(i<len)
    {
        if(j==-1||str[i]==str[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}
int main()
{
    while(~scanf("%s",str))
    {
        if(str[0]==.) break;
        len=strlen(str);
        make();
        int k=next[len];
        if(!(len%(len-k)))
            printf("%d\n",len/(len-k));
        else
            printf("1\n");
    }
    return 0;
}

 

POJ 2406 Power String

标签:str   std   main   poj   names   输出   bubuko   scanf   最小   

原文地址:https://www.cnblogs.com/greengenius/p/9241362.html

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