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

POJ 2406 Power Strings (求字符串循环节出现的次数)

时间:2016-08-23 16:41:12      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 44217   Accepted: 18449

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3
/*
 * POJ 2406 Power Strings
 * 求字符串循环节出现的次数
 * 
 * 结论:字符串S有最小循环节(p<len)的充要条件是(len-next[len])|len,next[len]>0
 * 其中p是循环节长度,len是字符串S的长度
 */

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int MAXN = 1000000+10;

void Get_Next(char* P,int* next)
{
    int i=0,k=-1;
    int plen=strlen(P);
    next[0]=-1;
    while(i<plen)
    {
        if(k==-1||P[k]==P[i])
        {
            i++,k++;
            next[i]=k;
        }
        else k=next[k];
    }
}
char str[MAXN];
int nxt[MAXN];

int main()
{
    while(scanf("%s",str)==1)
    {
        if(str[0]==.) break;
        Get_Next(str,nxt);
        int n=strlen(str);
        int k=n-nxt[n];
        if(n%k==0&&n/k>1) printf("%d\n",n/k);
        else printf("1\n");
    }
    return 0;
}

 

POJ 2406 Power Strings (求字符串循环节出现的次数)

标签:

原文地址:http://www.cnblogs.com/wangdongkai/p/5799689.html

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