kmp算法 匯總
阿新 • • 發佈:2017-07-18
target while 等於 ++ 都是 自身 sin pri back
來源:http://blog.csdn.net/qq_34494458/article/details/75253466
KMP算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,其對於任何模式和目標序列,都可以在線性時間內完成匹配查找,而不會發生退化,是一個非常優秀的模式匹配算法。
/* * next[]的含義(前提):x[i-next[i]...i-1] = x[0...next[i]-1]這很重要; * next[i]為滿足x[i-z...i-1] = x[0...z-1]的最大值z(就是x的自身匹配); */ //求next的代碼實現 /* * next[]求到了next[m],這個next[m]作用還很大;*/ void kmp_pre(char x[], int m, int next[]) { int i, j; j = next[0] = -1; i = 0; while (i < m) { while (-1 != j && x[i] != x[j]) j = next[j]; //j = -1,表示第0位都沒匹配成功;那就要直接推進一位; next[++i] = ++j; } } /* *還可以有一個小優化; */ void preKMP(char x[], intm, int kmpNext[]) { int i, j; j = kmpNext[0] = -1; i = 0; while (i < m) { while (-1 != j && x[i] != x[j]) j = kmpNext[j]; if (x[++i] == x[++j]) kmpNext[i] = kmpNext[j]; else kmpNext[i] = j; /*這個if很6,這除去了一些無意義的next[],大概意思是 *如果x[j]匹配失敗了,那麽就執行 j = next[j]; *而x[j] = x[next[j]]所以x[next[j]]肯定也會匹配失敗。 *所以就說這個next[j]是無意義的。*/ } } /* *x與y匹配; *返回x在y中出現的次數,可以重疊 *與求next[]函數的寫法基本相似; */ int next[10010]; int KMP_Count(char x[], int m, char y[], int n) { //x是模式串,y是主串; int i, j; int ans = 0; //preKMP(x, m, next); kmp_pre(x, m, next); i = j = 0; while (i < n) { while (-1 != j && y[i] != x[j]) j = next[j]; i++; j++; if (j >= m) { ans++; j = next[j]; } } return ans; }
經典題目:
看他的博客吧:http://blog.csdn.net/guhaiteng/article/details/52108690 加一個題目:http://poj.org/problem?id=3167/* *模式串可以浮動的模式串匹配問題 *給出模式串的相對大小,需要找出模式串匹配次數和位置 *比如說模式串: 1,4,4,2,3,1 而主串:5,6,2,10,10,7,3,2,9 *那麽子串:2,10,10,7,3,2就是和模式串匹配的。 *思路:只需比較前面比當前數小的數與等於當前數的數的個數就好了,看這兩個東西是否相等來進行kmp。 */ //#include<bits/stdc++.h> #include <iostream> #include <string> #include <queue> #include <map> #include <cstring> #include <cstdio> #include <vector> using namespace std; typedef long long LL; #define lson k<<1, ll, mid #define rson k<<1|1, mid+1, rr const int MAXN = 100008; int n, k, s, next[MAXN>>2], as[MAXN][26], bs[MAXN>>2][26], a[MAXN], b[MAXN]; vector<int> ans; void init() {//把輸入的字符串同化成as和bs; scanf("%d%d%d", &n, &k, &s); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i != 0) { for(int j = 1; j < 26; j++) as[i][j] = as[i-1][j]; } as[i][a[i]]++; } for(int i = 0; i < k; i++) { scanf("%d", &b[i]); if (i != 0) { for(int j = 1; j < 26; j++) bs[i][j] = bs[i-1][j]; } bs[i][b[i]]++; } } //這裏是沒有嵌套while循環的寫法,都是一樣的。 void build_next() { next[0] = -1; next[1] = 0;//這裏皮了一下 int j = 0, i = 1; while (i < k) { int t11 = 0, t12, t21 = 0, t22; for(int t = 1; t < b[i]; t++) if (i == j) t11 += bs[i][t]; else t11 += (bs[i][t]-bs[i-j-1][t]); if (i == j) t12 = bs[i][b[i]]; else t12 = bs[i][b[i]]-bs[i-j-1][b[i]]; for(int t = 1; t < b[j]; t++) t21 += bs[j][t]; t22 = bs[j][b[j]]; if (t11 == t21 && t12 == t22) next[++i] = ++j; else j = next[j]; } } void kmp() { ans.clear(); build_next(); int i = 0, j = 0; while (i < n) { int t11 = 0, t12, t21 = 0, t22; for(int t = 1; t < a[i]; t++) if (i == j) t11 += as[i][t]; else t11 += (as[i][t]-as[i-j-1][t]); if (i == j) t12 = as[i][a[i]]; else t12 = as[i][a[i]]-as[i-j-1][a[i]]; for(int t = 1; t < b[j]; t++) t21 += bs[j][t]; t22 = bs[j][b[j]]; if (t11 == t21 && t12 == t22) { ++i; ++j; if (j >= k) { ans.push_back(i-j+1); j = next[j]; } } else j = next[j]; } } int main() { //freopen("in.txt", "r", stdin); init(); kmp(); printf("%d\n", s = ans.size()); for(int i = 0; i < s; i++) printf("%d\n", ans[i]); return 0; }
kmp算法 匯總