题意:让你找最小重复串的个数
加深KMP中对next数组的理解
#include <cstdio>
#include <cstring>
using namespace std;
int next[1000500],slen;
char s[1000500];
void get_next(){
int i=1,j=0;
next[1]=0;
while(i<=slen){
if(j==0||s[i]==s[j])
next[++i]=++j;
else j=next[j];
}
}
int main(){
while(scanf("%s",s+1)&&s[1]!='.'){
slen=strlen(s+1);
get_next();
if(slen%(1+slen-next[slen+1])==0)
printf("%d
",slen/(1+slen-next[slen+1]));
else puts("1");
}
}
#include <cstdio>
#include <cstring>
using namespace std;
int next[1000500],slen;
char s[1000500];
void get_next(){
int i=0,j=-1;
next[0]=-1;
while(i<=slen){
if(j==-1||s[i]==s[j])
next[++i]=++j;
else j=next[j];
}
}
int main(){
while(scanf("%s",s)&&s[0]!='.'){
slen=strlen(s);
get_next();
if(slen%(slen-next[slen])==0)
printf("%d
",slen/(slen-next[slen]));
else puts("1");
}
}