zoukankan      html  css  js  c++  java
  • HDU 3746

    http://acm.hdu.edu.cn/showproblem.php?pid=3746

    kmp的Nxet数组求字符串循环节例题

    lenB%(lenB-Next[lenB])==0则其有周期lenB/(lenB-Next[lenB]),其中最小循环节长度是lenB-Next[lenB]

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std ;
    char B[100005] ;
    int Next[100005],lenB ;
    void GetNext()
    {
        int i,j ;
        Next[1]=0 ;  
        j=0 ;
        for(i=2 ;i<=lenB ;i++)
        {
            while(j>0 && (B[j+1]!=B[i]))j=Next[j] ;
            if(B[j+1]==B[i])j++ ;
            Next[i]=j ;
        }
    }
    int main()
    {
        int t ;
        scanf("%d",&t) ;
        while(t--)
        { 
            scanf("%s",B+1) ;
            lenB=strlen(B+1) ;
            GetNext() ;
            int minn=lenB-Next[lenB] ;
            if(!Next[lenB])printf("%d
    ",lenB) ;
            else if(lenB%minn)
                printf("%d
    ",minn-lenB%minn) ;
            else
                printf("0
    ") ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    20210312
    20210311
    20210310
    例5-1
    例5-2
    例4-12-2
    例4-12
    例4-11
    例4-10
    例4-9
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/3739849.html
Copyright © 2011-2022 走看看