KMP algorithm Learning record----Jake Boxer Blog Learning Section

Source: Internet
Author: User

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

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

How to use partial match tables

1. Function: Use partial match table to skip the old comparison part that has already been done, starting from the non-repeating match part.

2.if (match table result greater than 1)

{

Number of characters allowed to skip = match Length-partial match table [match length-1];

}

Else

{

Number of characters allowed to skip = partial match table [match length-1];

}

To illustrate:

Use "ABABABCA" as a substring to match the "Bacbababaabcbab" string.

Some of the matching tables are as follows:

String A|b|a|b|a|b|c|a

Index value 0|1|2|3|4|5|6|7

Match value 0|0|1|2|3|4|0|1

First time match

Bacbababaabcbab

Abababca

Result: No matching results

Second match

Bacbababaabcbab

Abababca

Result: The number of matches is: 1

Number of characters allowed to skip = partial match table [1-1] = 0;

Fifth time Matching

Bacb|ababa|abcbab

|ababa|bca

Result: The number of matches is: 5

Number of characters allowed to skip = 5-Partial match table [5-1] = 5-3 = 2;

Sixth time Matching

Bacbab|aba|abcbab

Xx|aba|babca

After skipping two characters, the match result is as above, this match result is: 3

Number of characters allowed to skip = 3-Partial match table [3-1] = 3-1 = 2;

Seventh time Matching

Bacbabab|a|abcbab

Xx|a|bababca

After skipping two characters, the match result is as above, this match result is: 1

At this point, the tail character of the substring has exceeded the length of the main string, so there is no matching result between the main string and the substring.

The next article is the KMP algorithm analysis of the big liar data structure.

KMP algorithm Learning record----Jake Boxer Blog Learning Section

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.