From the perspective of finite state machine to understand Knuth-morris-pratt algorithm (also known as KMP algorithm, "See the Pornography" algorithm)

Source: Internet
Author: User

Reprint please add:http://www.cnblogs.com/courtier/p/4273193.html

    • Before starting this article, the nagging words:

1: First, before reading this article, you should at least understand, what is the finite state machine, what is the KMP algorithm, because, this article is from the source of KMP, finite state

Machine, because, KMP is the DFA (deterministic finite automaton) on the simplification.

2: Many KMP articles (limited automata to explain very little), written in my opinion is not good enough for you if, without good mathematical foundation is difficult to understand them (for example),

Because, you can understand that the KMP algorithm can already enter the door of ACM Novice, many people say an algorithm can enter ACM? Let's see why it's called the KMP algorithm, very simple.

This was invented by three mathematicians, so I said that an algorithm would have to be inferred by three mathematicians to illustrate the weight.

3: Some people ask, I can give examples of the next way to do this kind of problem, I feel too, especially the computer professional, KMP algorithm also use an array to slowly move to others to see?

Suppose you're the interviewer and you're the one who moves slowly? Or the person who gave the DFA? Let's get to the point, there's a lot of crap.

(Introduction to Algorithms)

    • What we are going to do:

Is the finite automaton drawn,

(Handsome, it is a cool force, the string can be painted so handsome)

    • My method (unique, I find out through the calculation, but, without the mathematical formula)

1. First, draw a picture of the state diagram, for example, our input = {A,b,c} (because, our pattern string only abc three bits), constructs such as:

2. based on, create the following table: (that is, 0 inputs A to 1 such a relationship matrix):

        1. In 0 columns, why is 1? Very simple, because 0 input A to 1, input the other is to 0
        2. Here we have to set an X, this x∈ (0,t.len-1) {where T is a string}
        3. These fixed values are the values of the table below and cannot be replaced, as you'll see below.
0 1 2 3 4 5
A B A B A C
A 1 3 5
B 2 4
C 6

3. in the above we set an X (this is the key in the key) let's see how to use it?

1. How did the two x=0 come from? The first one, without looking, must be 0. The second one, we're going to see

The current column represents the letter (that is, the first letter of the column) and the value of the previous x is the same column as the current letter.

is the value of x (for example, the 1th column of the letter is B, the previous x is 0,so, see the No. 0 column of "B" is 0), so, No. 0

The value of column B is assigned to the forefront, i.e. x=0

2. Assign the column represented by the previous x to the forefront. Into the next.

Example 1

X=0 X=0
0 1 2 3 4 5
A B A B A C

A

1

1

3

5

B

0

2

 

4

 

 

C

0

0

6

Example 2

X=0 X=0 X=1
0 1 2 3 4 5
A B A B A C
A 1 1 3 5
B 0 2 0 4
C 0 0 0 6

4. The whole sheet is as follows:

X=0 X=0 X=1 x=2 X=3 X=0
0 1 2 3 4 5
A B A B A C
A 1 1 3 1 5 1
B 0 2 0 4 0 4
C 0 0 0 0 0 6
5. Drawing:
    • Above said so many, if, you do not understand, but also can see my translation from the course video (English is not good not to blame) code, please see my github:

Https://github.com/aliencool/Algorithm/tree/master/Searching

    • Conclusion:

Why are many people called KMP algorithm called "Look at the pornography" algorithm, because it is pinyin? Too low to explain this, why? Because, KMP he does not backtrack, every time, is

Pattern strings themselves (you know), so it's called "Look at the pornography" algorithm.

If you have questions or other, please contact me, please look forward to my next article may be about inverted index or basic theory of the algorithm or code, thank you, O (∩_∩) o!

From the perspective of finite state machine to understand Knuth-morris-pratt algorithm (also known as KMP algorithm, "See the Pornography" algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.