Period POJ - 1961

可有定义得到到i - nxt[i] 为循环节 且要使之能够整除

#include 
 #include 
 using namespace std;
 const int MAX_N = 1000100;
 const int MAX_M = 10010;
 #define sci(num) scanf("%d",&num)
 
     
      char
      str[MAX_N];
 int nxt[MAX_N];
 int lents;
 void kmp_pre(
     
      char
     * t) {
     nxt[0] = -1;
     for (int i = 0,k = -1;i < lents;) {
         if (k == -1 || t[i] == t[k]) {
             nxt[++i] = ++k;
         } else {
             k = nxt[k];
         }
     }
 }
 
 int main() {
     int cs =1;
     while(~sci(lents)){
         if (lents == 0) break;
         scanf("%s",str);
         printf("Test case #%d\n",cs++);
         kmp_pre(str);
         for (int i = 2;i <= lents;i++) {
             if (nxt[i] > 0 && i % (i - nxt[i]) == 0) {
                 printf("%d %d\n",i,i / (i - nxt[i]));
             }
         }
         printf("\n");
 
     }
     return 0;
 }

你可能感兴趣的:(KMP)