Loading

KMP算法模板

/*
KMP算法
by KONE
*/

#include<iostream>
#include<string>
using namespace std;

int* getNxte(char p[], const int size){
    int* next = new int[size];
    next[0] = -1;        //最开头的字符串设置为-1
    int k = -1, j = 0;
    while (j < (size - 1)){
        if (k == -1 || p[j] == p[k]){
            next[++j] = ++k;
        }
        else{
            k = next[k];
        }
    }
    return next;
}

 

posted @ 2018-10-22 10:22  青山新雨  阅读(118)  评论(0编辑  收藏  举报