KMP algorithm-from beginner to advanced

Source: Internet
Author: User

Title Description

Given a text string, text and pattern string pattern, find the first occurrence of the pattern string from the text string

First, the simplest way to understand the problem, that is, the solution of violence

Brute Force Solution

Enlarge the diagram above to get the following. The topic requires matching to the entire string, starting with the matching consideration.

The first element of the pattern string is used to match each element of the text string, and if it matches, it is matched backwards until all the pattern strings have been successfully matched.

If there is a mismatch in the pattern string, the pattern returns to the first element that matches the next element in test.

It should be noted here that the last element of the pattern string element that needs to be matched is text-j, because if it matches to the last, the pattern string is meaningless than the text length.

Throughout the process, you can imagine that the pattern string is aligned with the text first, then shifted back one bit relative to the text, dragging pattern, each move comparing the entire pattern pattern string to each element (understanding this helps to analyze later)

The key code is as follows

int search (const char*s, const CHAR*P) {    int i = 0;//used to mark the position of match to text string    int j = 0;//tag pattern string match position    int size = (int) Strlen (p);    int nlast = (int) strlen (s)-size; Here is a match to the longest position in text    while ((I <=  nlast) && (J < size))    {        if (s[i+j] = = P[j])        {            J + +;        }        else{            i++;            j = 0; J Return to the pattern string first element        }    }    if (j >= size)        return i;    return-1;}

The text length is n,pattern length of M. In this method, the time complexity is O (m*n) and the space complexity is O (1)

Further analysis:

In the brute force solution, why does the index J of the pattern string backtrack? The reason is that the pattern string needs to match the whole in order to know if it matches exactly.

So, add a condition if the character 22 of the pattern string is not equal. It also means that the pattern string can be dragged back to its own length by dragging it backwards if the length of the entire J does not match.

That is, if a mismatch occurs, move backward i+j positions to begin matching.

The time complexity of the entire algorithm is now degraded to O (N), but this is the result of the pattern string 22. Can this condition be weakened?

Of course it is possible, after weakening the condition becomes: the pattern string first character and other characters are not equal.

KMP algorithm-from beginner to advanced

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.