Extended KMP AlgorithmWhat is an extension KMP?
- The extended KMP is the longest common prefix length for each suffix of the pattern string and the main string.
- The extended KMP algorithm is an algorithm that uses the previously known conditions to reduce redundant matches and achieve shorter time.
- The purpose of the extended KMP algorithm is to get the next array and the extend array.
- Next[i] represents the beginning of my own position. The number of characters that the pattern string T matches itself.
- extend[I] indicates the number of characters that the pattern string T matches the main string s, starting with the first bit of the main string s.
< Span style= "line-height:28px" > The idea of extending the KMP algorithm:
-
- a : When the next value or extend value is currently being evaluated, p at the maximum position.
- p : The largest position in the current comparison. p=a+next[a]-1 or p=a+extend [a]-1 . It's not difficult to understand this equation.
- l : is the longest public prefix length of the current i-bit, calculated using the self-similarity of the pattern string T, but not necessarily equal to the actual longest public prefix length.
This is just a conclusion based on the data that has been matched. There is no match after the place who also might. Maybe there's no match for the same place.
- in other words extend[i]>=l. or next[i]>=l.
- l=next[i-a] . Note whether the next or the extend is the same equation.
-
- next [0] equals the length of the string T.
-
-
-
Infers whether the longest common substring obtained through the pattern string self-similarity is more than the largest position ever compared.
- If set, continue to the rear no more than the position of comparison. If a match is added to 1, the value is next[I] until the mismatch is reached.
- < span>
- Extend array:
- [0] The extend is calculated by comparison.
- The extend value of the 1~n is then evaluated one by one.
- Infer whether L+I-1>=P is established.
That is to infer whether the longest common substring obtained by the pattern string self-similarity is more than the maximum position that has been compared.
- If established. Continue to the rear no more than the position of comparison. If a match is added to 1, the value is extend[I] until the mismatch is reached.
- If not, extend[I]=l.
Extended KMP Code:
<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <string.h>using namespace Std;int extend[100001],next[100001];void exkmp ( Char *s,char *t) {int slen,tlen,a=0,i,l,p,j; Slen=strlen (s); Tlen=strlen (t); next[0]=tlen;//the next array while (A<tlen-1 && t[a]==t[a+1]) a++; Next[1]=a; A=1; for (i=2;i<tlen;i++) {p=a+next[a]-1; L=NEXT[I-A]; if (i+l-1>=p) {j= (p-i+1) >0?p-i+1:0; while (I+j<tlen && T[i+j]==t[j]) j + +; Next[i]=j; A=i; } else next[i]=l; }//extend array a=0; while (A<tlen && a<slen && t[a]==s[a]) a++; Extend[0]=a; a=0; for (i=1;i<slen;i++) {p=a+extend[a]-1; L=NEXT[I-A]; if (i+l-1>=p) {j= (p-i+1) >0?p-i+1:0; while (I+j<slen && j<tlen && s[i+j]==t[j]) j + +; Extend[i]=j; A=i; } else extend[i]=l; }}int Main () {//detailed analysis of details.return 0;} </span>
Extended KMP algorithm