题意:求一个字符串的最短的重复子串;
解法:KMP求出Next数组,如果len-Next[len]是len的约数的话,
那么s[len-Next[len]]-s[len-1]就是了,否则重复子串就是母串本身。这个由Next的性质很好推得。
代码:
/****************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
using namespace std;
#define eps 1e-8
typedef long long LL;
int Next[1100000];
char s[1001000];
int len=0;
int main()
{
while(scanf("%s",s)==1)
{
if(s[0]=='.')
break;
len=strlen(s);
int i=0,j=-1;
Next[0]=-1;
while(i<len)
{
while(j!=-1&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
int t=len-Next[len];
int ans=0;
if(len%t==0)
ans=len/t;
else ans=1;
cout<<ans<<'\n';
}
return 0;
}