Program (already accepted):
#include <stdio.h> #include <string.h> #include <memory.h> #define N 1000010char str[n];int next[n]; int Get_next (char pattern[], int next[]) { int j=0,k=-1; int Len=strlen (pattern); Next[0]=-1; while (J<len) { if (k==-1| | Pattern[j]==pattern[k]) next[++j]=++k; else k=next[k]; } j=len-k;//if the last position does not match, it will roll to the len-k position, which is the length of the minimum repeating string. if (len%j==0) return len/j; else return 1;} int main () {while (scanf ("%s", str), str[0]!= '. ') { printf ("%d\n", Get_next (str, next)); } return 0;} </span><strong style= "FONT-SIZE:18PX; Color:rgb (0, 0, 153); " ></strong>
Run Time:
The subject is still of great value:
1. The flexible application of KMP algorithm is investigated.
2. Because the 1000010*4 (int) will exceed the stack space (1M), the program crashes, so it can only do global variables.
POJ Brush Questions (2406)