POJ 2406 Power Strings

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26444   Accepted: 11072

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 <stdio.h>
#include <string.h>
using namespace std;
char str1[1000010];
int next[1000010];

void getnext(char str[])
{
    int k = -1;
    int j = 0;
    next[0] = -1;
    int nLen = strlen(str);
    while(j < nLen)
    {
        if (k == -1 || str[k] == str[j])
        {
            k++;
            j++;
            next[j] = k;
        }
        else
        {
            k = next[k];
        }
    }
}

int main()
{
    while(scanf("%s", str1) != EOF)
    {
        if (str1[0] == '.')
        {
            break;
        }
        getnext(str1);
        int nLen = strlen(str1);
        if (nLen % (nLen - next[nLen]) == 0)
        {
            printf("%d\n", nLen / (nLen - next[nLen]));
        }
        else
        {
            printf("1\n");
        }
    }
    return 0;
}

 

posted on 2013-06-02 16:54  lzm风雨无阻  阅读(195)  评论(0编辑  收藏  举报

导航