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


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

## LA3026 POJ1961 period 字符串周期(KMP) POJ2406 Power Strings

POJ 1961 http://poj.org/problem?id=1961  Period Description For each prefix of a given str...

## POJ 2406 Power Strings(KMP)

• cccruel
• 2017年08月05日 09:52
• 77

## [POJ]2406Power Strings KMP

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 49390   Accepted: 2...

## [poj 2406] Power Strings KMP *nex

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 40506 Accepte...
• ALPS233
• 2016年02月29日 14:07
• 335

## 【kmp】【后缀数组】【连续重复子串】Power Strings POJ2406

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25921 ...

## POJ 2406-Power Strings（重复子串-KMP中的next数组）

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47642   Accepted: 1...
• MIKASA3
• 2017年04月11日 14:52
• 247

## [KMP][后缀数组] poj2406 Power Strings

DescriptionGiven two strings a and b we define a*b to be their concatenation. For example, if a = “a...

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