终于靠着理解写出KMP了,两种KMP要代码中这种才能求循环节。i-next[i]就是循环节。

#include<cstdio>
#define N 1000005
char s[N];
int next[N];
void solve(){
    int ans;
    int k=-1,i=0;
    next[i]=k;
    while(s[i]){
        if(k==-1||s[i]==s[k]){//记住两个都是等于
            i++;
            k++;
            next[i]=k;
        }else
            k=next[k];
    }
    ans=i-next[i];
    if(i%ans)ans=1;
    else ans=i/ans;
    printf("%d\n",ans);
}
int main(){
    while(~scanf("%s",s)&&s[0]!='.')
        solve();
}

 

  

相关文章:

  • 2022-02-05
  • 2022-01-13
  • 2021-08-26
  • 2022-12-23
  • 2021-08-12
  • 2021-06-08
  • 2022-01-18
猜你喜欢
  • 2021-07-04
  • 2021-06-13
  • 2021-06-10
  • 2021-07-11
  • 2021-05-31
  • 2021-08-07
相关资源
相似解决方案