Algorithm-string matching-finite automaton Algorithm
Preface
In the previous article, we introduced the Rabin-Karp string matching algorithm. Here we will introduce the Finite Automata string matching algorithm and the Finite Automata algorithm) the most important character string matching algorithm is to calculate the transfer function. That is, if a current state k and A character x are given, the next state is calculated. The calculation method is to find the longest prefix of pattern pat, which is also pat [0... k-1] x (Note: The string subscript starts from 0) suffix, the length of the prefix is the next state. The matching process compares the status values of the input sub-string and the mode string. If they are equal, they exist. If they do not want to wait, they do not exist. For more information about the theoretical knowledge, see Introduction to algorithms.
Finite Automaton String Matching Algorithm
If the pattern string pat is m, the state value is 0-m, that is, m + 1, and the initial state is 0. Numbers = NO_OF_CHARS indicates the number of input tables. You can see from the source code below that the time complexity of the computed transfer function (preprocessing) is, and the matching time complexity is. This algorithm can be improved based on the KMP algorithm introduced later, and the process of solving the transfer function can be improved to obtain better time complexity.
The following is the execution process of the automatic machine with the mode string P = abababaca.
Source code implementation
#include
#include
using namespace std;const int NO_OF_CHARS = 256;//the numbers of input alphabetint getNextState(const string &pat, int M, int state, int x){ // If the character c is same as next character in pattern, // then simply increment state if (state < M && x == pat[state]) return state+1; int ns, i; // ns stores the result which is next state // ns finally contains the longest prefix which is also suffix // in pat[0..state-1]c // Start from the largest possible value and stop when you find // a prefix which is also suffix 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;} /* This function builds the TF table which represents Finite Automata for a given pattern */void compute_Transition_Function(const string &pat, int M, int TF[][NO_OF_CHARS]){ int state, x; for (state = 0; state <= M; ++state) for (x = 0; x < NO_OF_CHARS; ++x)//for each charater c in the inout alphabet table TF[state][x] = getNextState(pat, M, state, x);} /* Prints all occurrences of pat in txt */void Finite_Automata_search(const string &pat, const string &txt){ int M = pat.length(); int N = txt.length(); int TF_len = M+1; //this is supported by C++11int TF[TF_len][NO_OF_CHARS];//the state of transform table, stores the states. compute_Transition_Function(pat, M, TF);//compute the state of Transition Function // Process txt over FA. int state=0;//inite the state for (int i = 0; i < N; i++) { state = TF[state][txt[i]]; if (state == M)cout<