Power Strings

问题 D: 4.5.17 Power Strings

时间限制: 3 Sec  内存限制: 64 MB
提交: 2995  解决: 921
[提交][状态][讨论版]

题目描述

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).
 

输入

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.
 

输出

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

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3
思路:
找到最小的重复序列
kmp中的next数组就是这个作用
#include  <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int pext[1000000] = {-5};
char s[1000000];
int len1;
void get_next(char *T,int *next)
{
    int k = -1;
    int j = 0;
    pext[j] = k;
    while(j < len1)
    {
        if((k == -1) || (T[j] == T[k]))
        {
            k++;
            j++;
            pext[j] = k;
        }
        else
        {
            k = pext[k];
        }
    }
}
 
int main()
{
    while(1)
    {
        scanf("%s",s);
        if(strcmp(s,".") == 0)
        {
            return 0;
        }
        else
        {
            int sum = 0;
            len1 = strlen(s);
            get_next(s,pext);
            int z; 
            z =  len1 - pext[len1];
            if(len1 % z ==0)
            {
                for(int i=1;i<=len1;i++)
                {
                    if(i%z==0)
                        sum++;    
                }
                printf("%d\n",sum); 
            }
            else
            {
                printf("1\n");
            }
        }
    }
    return 0;
} 

 

 
posted @ 2019-06-25 14:32  YFR718  阅读(257)  评论(0编辑  收藏  举报