An understanding of the KMP algorithm (i)

Source: Internet
Author: User
Tags first string

Locating a small substring in a large string is a pattern match for a string, which should be considered one of the most important operations in a string.

Problem Description:

There is a text string s and a pattern string p, now to find the position of P in S, how to find?

If the idea is to match with violence and assume that the text string matches to the I position, the pattern string matches to the J position.

Algorithm Description:

For the pattern matching of strings, first of all, one of the simplest algorithms is the brute force law. The specific algorithm describes:

(1) Initialize I to the initial position of the main string, which is assumed to be the 0 position of the main string, and J points to the 0 position of the substring.

(2) If the current character matches successfully, i.e. s[i] = = P[j], then i++,j++ continues to match the next string.

(3) If the match is unsuccessful, that is s[i]! = p[j], then i = i-j+1,j=0. The equivalent of every match failure, I backtracking, J reset to 0.

At the time of the unsuccessful match, I was the indicator that matched the first string, then the amount of the position moved before the start of the comparison match was J. So i-j is the starting position for this match. +1 represents a position to move backwards.

Code implementation:

//returns the position of the substring p in the main string s, and returns 1 if it does not exist .intViolentmatch (Char*s,Char*p) {    inti =0; intj =0; intSlen =strlen (s); intPlen =strlen (P);  while(I < Slen && J <Plen) {        if(S[i] = =P[j]) {            ++i; ++J; }        Else{i= I-j +1;//J represents the number of times this move, I-j represents the starting position of the current of the S string, and +1 represents a forward positionj =0; }    }// while    if(J = =Plen) {        returnIPlen; }    Else    {        return-1; }}

The time complexity of this brute-force matching algorithm is in the worst case of o[(n-m+1) *m], where n is the length of the main string, and M is the length of the pattern string. We can see that the time complexity of the algorithm is very large, obviously we do not need such an algorithm, because it sucks. But this is the basis for other algorithms that we design.

An understanding of the KMP algorithm (i)

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.