Learning Record---KMP algorithm-partial match table understanding

Source: Internet
Author: User

If you want to reprint, please keep this article link.

See Ruan a peak of the big God's string matching KMP algorithm, about part of the matching part is not very understanding, deliberately to see the English link of the great God article, here write their own understanding, used as a learning record.

Nandashin article link: https://kb.cnblogs.com/page/176818/

Jake Boxer English blog post link: http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/

The definition of prefixes and suffixes is ignored here.

I. Partial match table definition

Referring to the original sentence of Jake Boxer blog post: The length of thelongest proper prefix in the (sub) pattern, matches a proper suffix in the SAM E (sub) pattern.

Personal understanding is: the longest prefix and its subset with the longest suffix and its subset of matching results.

In layman's words: compare all prefixes with their corresponding suffixes, which will be compared to the results, and this table is called a partial match table.

So how the partial match table is calculated, the following explanation

Two. Calculate the partial match table

1. Set the main string: ABABABCA length is 8;

2. Compare the prefix to the suffix:

The length of the ① substring is 1 o'clock, the substring is: A, the prefix does not exist, and the partial match table results are:
|a|b|a|b|a|b|c|a|
|0|
The length of the ② substring is 2 o'clock, the substring is: AB, the prefix is exactly equal to the number of matches: 0, the partial match table results are:
The prefix is: A;
The suffix is: b;
|a|b|a|b|a|b|c|a|
|0|0|
The length of the ③ substring is 3 o'clock, the substring is: ABA, and the prefix is exactly equal to: 1, then the partial match table result is:
The prefix is: a;ab;
Suffix: a;ba;
The prefix a is the same as the suffix a, when the exact number of matches is 1;
|a|b|a|b|a|b|c|a|
|0|0|1|
The length of the ④ substring is 4 o'clock, the substring is: abab, and the prefix is exactly like the top number of matches: 1
The prefix is: A;ab;aba;
Suffix: b;ab;bab;
The prefix AB is the same as the suffix AB, where the exact number of matches is: 2, the reason is: the exact matching string length is: 2
|a|b|a|b|a|b|c|a|
|0|0|1|2|
By the same principle, the results of other substring matching are computed, and the substring length is 7 o'clock.
Substring of length 7: abababc
Prefix: a;ab;aba;abab;ababa;ababab;
Suffix: c;bc;abc;babc;ababc;bababc;
The exact number of matches is: 0
|a|b|a|b|a|b|c|a|
|0|0|1|2|3|4|0|
Finally, a full-length string is matched:
Prefix: A;AB;ABA;ABAB;ABABA;ABABAB;ABABABC;
Suffix: a;ca;bca;abca;babca;ababca;bababca;
The exact number of matches is: 1

Get the complete partial match table as:
|a|b|a|b|a|b|c|a|
|0|0|1|2|3|4|0|1|

First give yourself a flag, urging yourself to write KMP algorithm of the learning record, the end.


Learning Record---KMP algorithm-partial match table understanding

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.