题意:让你找最小重复串的个数

加深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\n",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\n",slen/(slen-next[slen]));
        else puts("1");
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2021-07-03
  • 2021-08-26
  • 2022-12-23
  • 2021-08-24
相关资源
相似解决方案