1. 程式人生 > 其它 >C++實現Kmp演算法

C++實現Kmp演算法

  • Kmp演算法:查詢子串
    • 字首和字尾公共最長子串長度就是next陣列的值
    • 當text和pattern串不等時,pattern利用next陣列進行回退,直到找到相等的字元或返回到開頭
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 
// shell , quick, kmp 
// pattern 
// 
// abcabc
// k = 0
// q = 1
void make_next(const char *pattern, int *next) {

    int q, k;
    int
m = strlen(pattern); next[0] = 0; // for (q = 1,k = 0;q < m; q ++) { while (k > 0 && pattern[q] != pattern[k]) { k = next[k-1]; } if (pattern[q] == pattern[k]) { // 如果字首與字尾有相同的字元 k ++; } next[q] = k; } }
int kmp(const char *text, const char *pattern, int *next) { int n = strlen(text); int m = strlen(pattern); make_next(pattern, next); int i, q; for (i = 0, q = 0;i < n;i ++) { //i --> text, q --> pattern #if 1 while (q > 0 && pattern[q] != text[i]) { q
= next[q-1]; } #endif if (pattern[q] == text[i]) { q ++; } // q == m ---> if (q == m) { return i-q+1; } } return -1; } int main() { char *text = "abcabcabcabcabcd"; char *pattern = "abcabcd"; int next[20] = {0}; int idx = kmp(text, pattern, next); printf("match pattern: %d\n", idx); }