poj 2406 Power Strings

来源:互联网 发布:网络侦探龙帝 编辑:程序博客网 时间:2024/06/06 06:37
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 33636 Accepted: 13973

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

abcdaaaaababab.

Sample Output

14

3

题目大意:给出一个字符串,求这个串是有几个子串构成的思路:用KMP算法中的next数组,这个字符串的next数组里边,字符串的总长度len减去字符串最后一个字符的下一个字符(就是'\0')所对应的next值next[len],就是子串的长度,next数组的值是根据字符串本身用一定规则得出,next数组存放的是如果匹配不成功要跳转到的位置,这个很巧妙,kmp算法,好好看看。2014,12,5

#include<stdio.h>#include<string.h>char x[1100000];int next[1100000];void getnext(char s[]){int len1,i,j;len1=strlen(s);i=0;j=-1;next[i]=j;while(i<len1){if(j==-1||s[i]==s[j]){++i;++j;next[i]=j;}else j=next[j];}}int main(){int t,len;while(scanf("%s",x)){getnext(x);if(x[0]=='.') break;else {len=strlen(x);t=len-next[len];if(len%t==0)printf("%d\n",len/t);else printf("1\n");}}return 0;}


0 0
原创粉丝点击