题目地址: http://poj.org/problem?id=2406
简单KMP————
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long LL;
const int N=1000009;
const LL II=100000000;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
int next[N],tlen,wlen;
char word[N];
void getnext(char *p)
{
int j=0,k=-1;
next[0]=-1;
while(j<wlen)
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
}
int main()
{
int i,j,T;
while(scanf("%s",word)&&strcmp(word,".")!=0)
{
wlen=strlen(word);
getnext(word);
int k=(wlen-next[wlen]);
if(wlen%k==0)
printf("%d\n",wlen/k);
else
printf("1\n");
}
return 0;
}