码迷,mamicode.com
首页 > 其他好文 > 详细

From DFA to KMP algorithm

时间:2020-06-05 15:09:25      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:following   you   sam   situation   get   serve   str   branch   represent   

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

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:

  1. The column index of dfa[][] is status of the dfa, it isn‘t the status of the string but abstracted from the string
  2. The row index of dfa[][] is characters in the string,
  3. 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

标签:following   you   sam   situation   get   serve   str   branch   represent   

原文地址:https://www.cnblogs.com/wevolf/p/13049538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!