KMP algorithm-Minimum overwrite substring

Source: Internet
Author: User

 

KMP and minimum overwrite substring

Minimum overwrite substring: For a string s, its minimum overwrite substring refers to the substring P with the minimum length. P can obtain Q through its multiple connections, finally, we can make s a sub-string of Q.

For example:

For S = "abcab", its minimum overwrite substring P = "ABC", because P is followed by another P (that is, overlapping 0 characters ), Q = "abcabc" can be obtained. In this case, S is the sub-string of Q.

For S = "ababab", its minimum substring is P = "AB ".

According to the definition of the next array of the kmp algorithm, if the length of string S is set to N, next [N] = next [n-1], N-1 is the last bit of S.

Next [N] indicates s [0, 1, 2 ,..., next [N]-1] = s [n-next [N],..., n-1], set the two segments to S1 and S2 respectively.

If the sum of the lengths of S1 and S2 is less than the length of S, it means that S1 and S2 are non-overlapping prefixes and suffixes respectively, then the minimum overwrite substring must be the prefix obtained after S is intercepted by S2.

If the sum of S1 and S2 is greater than or equal to the length of S, the minimum overwrite substring must also be the prefix obtained after S is truncated to S2.

In both cases, we can conclude that the minimum overwrite substring is the prefix of S and its length is n-next [N].

 

Some of my understanding of KMP (distributed): the essence of pre [I] (or next [I]) is string STR [1 .. the "equal prefix and suffix" of the longest and smaller than I are STR [1 .. pre [I] (prefix) and STR [(I-pre [I] + 1 ).. i] (suffix). In general, STR [1 .. i] the maximum K value of the first K letters and the last K letters.

KMP algorithm details visible: http://blog.csdn.net/fjsd155/article/details/6864233

Another conclusion:

The minimum length of the substring to be overwritten is n-next [N] (n-pre [N]). N indicates the length of the substring.

The proof is divided into two parts:

1-The prefix of N-next [N] Must overwrite the substring.

When next [N] <n-next [N], A, Prefix A for next [N] and suffix B for next [N] are equal, therefore, the prefix C for n-next [N] Must overwrite the suffix B;


When next [N]> N-next [N], B shifts the original string X to n-next [N] units to get the y string. According to the definition of next, the suffix string a with the length of next [N] is equal to the prefix string B, and the prefix C with the length of N-next [N] in the X string is equal to the prefix d in the Y string, e In string X is equal to d in string y ...... The prefix C with the length of N-next [N] In string X can overwrite the entire string.


2-The length of N-next [N] is the shortest prefix.

C. String a is the prefix of N-next [N] and string B is the suffix of next [N, assume that the prefix C with a length less than N-next [N] can overwrite the entire string. Then, the original string X is truncated from the previous section C to obtain the new string y, then, y must be equal to the prefix where the original string length is greater than next [N], and the definition of the next array (so that STR [1 .. i] the maximum K value of the first K letters and the last K letters .) Conflict. Pass! Someone asked why is Y equal to the prefix of next [N] when the original string grows up? Assume that the composition of the original string must be ccc ...... E (E is the prefix of C), and the composition of string y must be CC ...... E (one less C than the original string), understand it!


 

The final conclusion is as follows: total length-next [N].

 

// Memory time // 1347 K 0 Ms //: snarl_jsb // 2014-10-02-21.08 # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue >#include <stack> # include <map> # include <string> # include <climits> # include <cmath> # define n 1000010 # define ll long longusing namespace STD; string STR; vector <int> next; void getnext () {int Len = Str. size (); next. push_back (0); int K = 0; For (INT I = 1; I <Len; ++ I) {If (K! = 0 & STR [I]! = STR [k]) k = next [k-1]; If (STR [I] = STR [k]) K ++; next. push_back (k) ;}} int main () {ios_base: sync_with_stdio (false); cin. tie (0); // freopen ("C: \ Users \ Asus \ Desktop \ cin. CPP "," r ", stdin); // freopen (" C: \ Users \ Asus \ Desktop \ cout. CPP "," W ", stdout); While (CIN> Str) {next. clear (); int Len = Str. size (); getnext (); // For (INT I = 0; I <Len; ++ I) /// {// cout <len-next [I] <Endl; //} cout <"minimum overwrite substring: "<len-next [len-1] <Endl;} return 0 ;}

  

KMP algorithm-Minimum overwrite substring

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.