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

97人阅读 评论(0)

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;
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：32899次
• 积分：2264
• 等级：
• 排名：第16699名
• 原创：184篇
• 转载：1篇
• 译文：0篇
• 评论：10条
阅读排行
评论排行
最新评论