# poj 2406 Power Strings-字符串（kmp）

Power Strings

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


i初始为0，j初始为-1。

 0 1 2 3 4 a b a b a

#include <cstring>
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#define INF 0x3f3f3f3f
const int maxn=50100;
using namespace std;
char s[1000000];
int  b[1000000];
int main()
{
while(scanf("%s",s) && s[0]!='.')
{
memset(b,0,sizeof(b));
int j=-1,i=0;
int len=strlen(s);
while(i<len)
{
i++;j++;

if(s[i]==s[j])
{
b[i]=b[j]+1;
}
else//周期不对就重新找
{
if(i>=len)
if(len%(i-j)==0)
break;

j=-1;
for(int k=0;k<=i;++k)
b[k]=0;
}
}
printf("%d\n",b[len-1]+1);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：poj 2406 Power Strings-字符串（kmp） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)