编辑代码

#include <stdio.h>
int main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
   void kmp() {
    char str1[] = "abababcabb";
    char str2[] = "abcab";

    int len2 = strlen(str2);
    int next[len2];
    next[0] = 0;
    _Bool flag = 1;

    while (flag) {
        int i = 0;
        for (int j = 1; j < len2; ++j) {
            if (str2[j] == str2[i]) {
                next[j] = i + 1;
                i++;
            } else{
                i = next[i-1];
                if (i!=0 && str2[i] == str2[j]){
                    next[j] = i + 1;
                    i++;
                } else{
                    next[j] = 0;
                    i = 0;
                }
            }
            if(j == len2 - 1)
                flag = 0;
        }
    }

    
    for (int i = 0; i <len2; ++i) {
        printf("%d",next[i])   ;
    }
}
}