POJ_2406_Power Strings[KMP]

来源:互联网 发布:怎么删掉淘宝购买记录 编辑:程序博客网 时间:2024/06/07 18:55
/*
Power Strings
Time Limit: 3000MS        Memory Limit: 65536K
Total Submissions: 47643        Accepted: 19867

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

题意:给出一个串,问你这个最多是多少个相同的字串重复连接而成的。如:ababab则最多有3个ab连接而成

*/

#include <cstdio>#include <cstring>using namespace std;const int MAX = 100000000+10;char s[MAX]; int next[MAX];void get_next(int len)//找到KMP中的NEXT数组 {int i=0,j=-1;next[0] = -1;while(i<len){if(j==-1 || s[j]==s[i]){i++; ++j;next[i] = j;}else{j = next[j];}}}int main(){while(scanf("%s",s) != EOF){if(s[0]=='.'){break;}int len = strlen(s);get_next(len);int ans = 1;int t =  len-next[len];if(len % t==0) {ans = len / t;} printf("%d\n",ans);}return 0;}


0 0