Expand The problem solved by KMP is to give two strings s and T, respectively, the length of N and M, and the maximum common prefix length for each suffix substring of s, and the longest public prefix of T, denoted as extend array, i.e. Extend[i] for s[i,n-1] (I starting from 0) and T.
It should be noted that if the longest common prefix length of extend[i]=m, i.e. s[i,n-1] and T is M (which is exactly the length of T), then it means that T finds a match in S and the starting position is I, which explains why this algorithm is called extension KMP.
In fact roughly and KMP has the same wonderful, all matches, is to borrow a next array.
Here is an example S= "Aaaabaa", t= "AAAAA", first, calculate extend[0], 5 matches are required until a mismatch occurs .
To know extend[0]=4, the following calculation extend[1], in the calculation of extend[1], whether you also need to like the calculation of extend[0] when the beginning of the match? The answer is no, because by calculating extend[0]=4, thus can be obtained s[0,3]=t[0,3], further can get s[1,3]=t[1,3], calculate extend[1], in fact, from the s[1] start matching, set auxiliary array next[i] means t[i , M-1] and T are the longest common prefix lengths. In this example, next[1]=4, i.e. t[0,3]=t[1,4], further gets t[1,3]=t[0,2], so s[1,3]=t[0,2], so in the calculation extend[1], by the calculation of extend[0], already know s[1,3]=t [0,2], so the first 3 characters do not need to match, directly match s[4] and t[3] can, at this time the mismatch occurs, so extend[1]=3. This example is representative and interested readers can continue to calculate the remaining extend arrays.
1. General steps to expand the KMP algorithm
by the above example, the idea of expanding the KMP algorithm is actually embodied, and the following describes the general steps to expand the KMP algorithm.
First we calculate the extend array from left to right , in a moment, set EXTEND[0...K] has been calculated, and the previous matching process reached the farthest position of P, the so-called farthest position, strictly speaking is the maximum value of i+extend[i]-1 (0 <=I<=K), and set this maximum position as PO, as in the previous example, calculate extend[1], p=3,po=0.
now to calculate extend[k+1], according to the definition of the extend array, you can infer S[po,p]=t[0,p-po], so as to get S[k+1,p]=t[k-po+1,p-po], so len=next[k-po+1], ( Recall next the definition of the array), in two different situations to discuss:
first case:k+len<p
As shown in the following:
in which S[k+1,k+len]=t[0,len-1], then s[k+len+1] must not be equal to T[len], because if they are equal, there is S[k+1,k+len+1]=t[k+po+1,k+po+len+1]=t[0,len], then next [K+po+1]=len+1, this does not correspond to the definition of the next array (Next[i] represents the longest common prefix length for t[i,m-1] and T), so in this case you know extend[k+1]=len without any matching.
second case: k+len>=p
Such as:
in which S[p+1] After the characters are unknown, that is, the string has not been matched, so in this case, it is necessary to start from s[p+1] and T[p-k+1] to match until the mismatch occurs, when the match is complete, if the resulting extend[k+1]+ (k+1) Greater than p to update the unknown p and PO.
at this point, the process of extending the KMP algorithm has been described, and the reader may find that the next array is not yet documented, and in fact, the process of computing the next array is exactly the same as the process of calculating extend[i], as if it were a T-string, T is a special extension of the string KMP algorithm matching can be, the calculation process of the next array is already calculated, so according to the algorithm described above to calculate next array can, here no longer repeat.
2. Time complexity Analysis
The following analysis of the algorithm's time complexity, through the above algorithm introduction can be known, for the first case, do not need to do any match to calculate the Extend[i], for the second case, is never matched position to start matching, matched position no longer match, That is, for each position of the mother string is only matched once, so the overall time complexity of the algorithm is O (n), and in order to calculate the auxiliary array Next[i] need to first expand the string T KMP algorithm processing, so the overall complexity of the expansion KMP algorithm is O (n+m). where n is the length of the female string, and M is the length of the substring.
3. Templates
1#include <cstdio>2#include <cstring>3 Const intMAXN =100010;4 Chars[maxn]="Aaaabaa", T[MAXN] ="AAAAA";5 intNEXTT[MAXN], ls, lt, Extend[maxn];6 7 voidGet_next ();8 voidEXKMP ();9 Ten intMain () One { Als = strlen (s);//do not use global or local chaos -lt =strlen (t); - the EXKMP (); - for(inti =0; I < lt; i++){ -printf"%d", Nextt[i]); - } +Puts""); - + for(inti =0; i < LS; i++){ Aprintf"%d", Extend[i]); at } -Puts""); - return 0; - } - - voidGet_next () { innextt[0] =lt; - to intK =1; + while(K < lt && t[k] = = T[k-1]) -k++; thenextt[1] =K; * $ intPO =1;Panax Notoginseng for(k =2; K < lt; k++){ - if(Nextt[k-po] + K < Nextt[po] +po) theNEXTT[K] = nextt[k-PO]; + Else{ A intj = Nextt[po] + po-K; the if(J <0) +j=0; - while(j + K < lt && t[j] = = T[j +K]) $J + +; $ -NEXTT[K] =J; -PO =K; the } - }Wuyi } the - voidexkmp () { Wumemset (NEXTT,0,sizeof(NEXTT)); - Get_next (); About $ intK =0; - while(K < ls && k < lt && s[k] = =T[k]) -k++; -extend[0] =K; A + intpo=0; the for(k =1; K < LS; k++){ - if(Nextt[k-po] + K < Extend[po] +po) $EXTEND[K] = nextt[k-PO]; the Else{ the intj = Extend[po] + po-K; the if(J <0) thej =0; - while(k + J < ls && J < lt && S[k + j] = =T[j]) inJ + +; the theEXTEND[K] =J; AboutPO =K; the } the } the}
4. Template Exercises
https://cn.vjudge.net/problem/HDU-3613
Reference Questions for exercises:
5. Refer to the original blog link:
41831947
Expand the KMP algorithm detailed