From DFA to KMP algorithm
Posted wevolf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了From DFA to KMP algorithm相关的知识,希望对你有一定的参考价值。
From DFA to KMP algorithm
DFA
In the theory of computation, a branch of theoretical computer science, a deterministic finite automaton (DFA)—also known as deterministic finite acceptor (DFA), deterministic finite-state machine (DFSM), or deterministic finite-state automaton (DFSA)—is a finite-state machine that accepts or rejects a given string of symbols, by running through a state sequence uniquely determined by the string. So, the most important in it is State transition.
DFA and String
DFA does not directly represent a state of the string, but represents a state abstracted from the string, this is an important issue that you need to understand.
KMP algorithm Fundamental
Brute-force substring search
The idea of the KMP algorithm needs to be understood on the basis of violent matching, mainly focusing on the mismatch during the matching process. We assume that there is a text string Text_str and a matching string Pat_str. When using Pat_str to match a text string, Assuming that the part we match is pat_str[0] to pat_str[j], in Text_str, pat_str[i] does not match Text_str[j], then the following situation.
If we directly follow the original method of violence, we will directly move Pat_str one bit back, but while we scan to Text_str[i] we get wrong, this means that we have know the Text_str from Text_str[i-j] to Text_str[i], and we also know what Pat_str stored, so we could know this two parts‘ relationship, also it must be some complex. So let‘s consider what the relationship between Text_str[i-j] to Text_str[i] and Pat_str[0] to Pat_str[j].
Backup
In the above problem, we observed that if we return from Text_str[1], we continue to match each time, then it will match at the following position
In the above, the character after Pat_str[2] won‘t be compare. And you will notice that suffixes of Text_str[i] and prefixes of Pat_str[0] is the same, this is the most problem.
For others conditions of Pat_str and Text_str, they won‘t have Matching fields of the Prefixes of Pattern_str.
DFA of a String Matching
I don‘t think I could interpreter this problem clearly, so I use a picture of a book to illustrate it, First of all, you must remember what dfa[][] in the blow picture and what dfa[][] save, in the blow picture:
- The column index of dfa[][] is status of the dfa, it isn‘t the status of the string but abstracted from the string
- The row index of dfa[][] is characters in the string,
- The number save in the dfa[i][A] represent when dfa in status (i) , get a character ‘A‘ from string, then next status of dfa[][] is dfa[i][A]
以上是关于From DFA to KMP algorithm的主要内容,如果未能解决你的问题,请参考以下文章
hdu, KMP algorithm, linear string search algorithm, a nice reference provided