模式搜索的有限状态机算法的 C++ 程序
在本文中,我们将讨论用于模式搜索的有限状态机算法的程序。
我们提供了 text[0...n-1] 和 pattern[0...m-1]。我们必须在 text[] 中找到 pattern[] 的所有出现位置。
为此,我们将预处理 text[] 并构建一个二维数组来表示它。此后,我们只需在 text[] 的元素和自动机的不同状态之间遍历即可。
范例
#include<stdio.h> #include<string.h> #define total_chars 256 int calc_nextstate(char *pat, int M, int state, int x) { if (state < M && x == pat[state]) return state+1; int ns, i; for (ns = state; ns > 0; ns--) { if (pat[ns-1] == x) { for (i = 0; i < ns-1; i++) if (pat[i] != pat[state-ns+1+i]) break; if (i == ns-1) return ns; } } return 0; } //builds Finite Automata void calc_TF(char *pat, int M, int TF[][total_chars]) { int state, x; for (state = 0; state <= M; ++state) for (x = 0; x < total_chars; ++x) TF[state][x] = calc_nextstate(pat, M, state, x); } //prints all occurrences of pattern in text void calc_occur(char *pat, char *txt) { int M = strlen(pat); int N = strlen(txt); int TF[M+1][total_chars]; calc_TF(pat, M, TF); int i, state=0; for (i = 0; i < N; i++){ state = TF[state][txt[i]]; if (state == M) printf ("\n Given pattern is found at the index%d",i-M+1); } } int main() { char *txt = "AABCDAABBDCAABADAABDABAABA"; char *pat = "AABA"; calc_occur(pat, txt); return 0; }
输出
Given pattern is found at the index 11 Given pattern is found at the index 22
广告