关闭

POJ 2406 - Power String

标签: ACMPOJ
62人阅读 评论(0) 收藏 举报
分类:

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


题意:给出一个周期字符串,求出其中子字符串循环的次数。

把第一个子字符串作为模式,求出长度,再进行计算即可。

#include <cstdio>
#include <cstring>

char str[1000005];
int tar[1000005];

int main()
{
    while (scanf("%s", str) != EOF)
    {
        if (strcmp(str, ".") == 0)
            break;
        int len = strlen(str);
        tar[0] = 0;

        int cur = -1;
        for (int i = 1; i < len; ++i)
        {
            while (cur > 0 && str[cur+1] != str[i])
                cur = -1;
            if (str[cur+1] == str[i])
                cur++;
            tar[i] = cur;
        }
        if (len % (len - tar[len - 1] -1) == 0)
            printf("%d\n", len / (len - tar[len - 1] - 1));
        else
            printf("1\n");
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21853次
    • 积分:1724
    • 等级:
    • 排名:千里之外
    • 原创:157篇
    • 转载:2篇
    • 译文:0篇
    • 评论:7条
    文章分类
    最新评论