First glue when I see the blog entry KMP Gangster: orz orz
From start to finish a thorough understanding of KMP
I think this has been said very detailed, and kept on reading.
Template is given below:
next array Seeking:
. 1 void GetNext ( char * P, int Next []) 2 { . 3 int pLen = strlen (P); . 4 Next [ 0 ] = - . 1 ; . 5 int K = - . 1 ; . 6 int J = 0 ; . 7 the while (J <pLen - . 1 ) . 8 { . 9 // P [K] denotes the prefix, p [j] represents the suffix 10 IF (K == - . 1 || P [J] == P [K]) . 11 { 12 is ++k; 13 ++j; 14 next[j] = k; 15 } 16 else 17 { 18 k = next[k]; 19 } 20 } 21 }
Seeking improved version next array:
. 1 // Next Array evaluation method after the optimization 2 void GetNextval ( char * P, int Next []) . 3 { . 4 int pLen = strlen (P); . 5 Next [ 0 ] = - . 1 ; . 6 int K = - . 1 ; . 7 int J = 0 ; . 8 the while (J <pLen - . 1 ) . 9 { 10 // P [K] denotes the prefix, p [j] suffixes . 11 IF (K == - . 1 || P [J] == P [ K]) 12 is { 13 is ++ J; 14 ++ K; 15 // than before next Array evaluation method, the following changes in four rows 16 IF (P [J] =! P [K]) . 17 ; next [J] = K // before only this line 18 the else 19 // because p [j] = p [next [j]] does not appear, so when there is need to continue recursively, k = the Next [k] = the Next [the Next [k]] 20 the Next [J ] = Next [K]; 21 is } 22 is the else 23 is { 24 K = Next [K]; 25 } 26 is } 27 }
KMP algorithm:
. 1 int KmpSearch ( char * S, char * P) 2 { . 3 int I = 0 ; . 4 int J = 0 ; . 5 int sLen = strlen (S); . 6 int pLen = strlen (P); . 7 the while (I <sLen J && < pLen) . 8 { . 9 // ① If j = -1, or the current character matching is successful (i.e., S [i] == P [j ]), have made ++ I, J ++ 10 IF (J == - . 1 | | S [I] == P [J]) . 11 { 12 is ++ i ; 13 is J ++ ; 14 } 15 the else 16 { . 17 // ② if j = -1, and the current character match fails, then let unchanged i, j = next! (i.e., S [i] = P [j ]!) [j] 18 is // next [j] is the value of j corresponding to the next . 19 j = next [j]; 20 is } 21 is } 22 is IF (j == pLen) 23 is return I - j; 24 the else 25 return - . 1 ; 26 }
With their own custom templates (negligible)
1 int Next[1000010]; 2 char str1[1000010]; 3 char str2[1000010]; 4 5 void getnext(char *str) 6 { 7 int len=strlen(str); 8 int j=0; 9 int k=-1; 10 Next[0]=-1; 11 while(j<len) 12 { 13 if(k==-1||str[j]==str[k]) 14 { 15 j++; 16 k++; 17 if(str[j]!=str[k]) 18 { 19 Next[j]=k; 20 } 21 else 22 Next[j]=Next[k]; 23 } 24 else 25 k=Next[k]; 26 } 27 } 28 29 int KMP(char *str1,char *str2) 30 { 31 int len1=strlen(str1); 32 int len2=strlen(str2); 33 int i=0; 34 int j=0; 35 while(i<len1) 36 { 37 if(j==-1||str1[i]==str2[j]) 38 { 39 i++; 40 j++; 41 } 42 else 43 { 44 j=Next[j]; 45 } 46 47 } 48 if(j==len2) 49 return i-j; 50 else 51 return -1; 52 }
Template think too long? Look at it this way:
https://www.cnblogs.com/eternhope/p/9481643.html
Here are some examples:
POJH-3461 Oulipo
http://poj.org/problem?id=3461
Description
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:
All had normal pair, but asserted itself false. All had normal Fair, first, then arose the inhuman, the maddening. He wanted to know where was based association that united the novel stir his carpet assailant at any moment his imagination, intuition of a taboo, the vision of a dark evil of what vacant , an unsaid: vision, a commander avision forgetting everything, where abolished the reason everything looked normal but ...
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
- One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
- One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN
Sample Output
1 3 0
Meaning of the questions: Find the number of substring A string that appears in the B
Standard KMP is to return the substring position of the first occurrence, this statistic is the number of times, look like a little change
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <string> 5 #include <vector> 6 using namespace std; 7 int Next[1000010]; 8 char str1[1000010]; 9 char str2[1000010]; 10 11 void getnext(char *str) 12 { 13 int len=strlen(str); 14 int j=0; 15 int k=-1; 16 Next[0]=-1; 17 while(j<len) 18 { 19 if(k==-1||str[j]==str[k]) 20 { 21 j++; 22 k++; 23 if(str[j]!=str[k]) 24 { 25 Next[j]=k; 26 } 27 else 28 Next[j]=Next[k]; 29 } 30 else 31 k=Next[k]; 32 } 33 } 34 35 int KMP(char *str1,char *str2) 36 { 37 int ans=0; 38 int len1=strlen(str1); 39 int len2=strlen(str2); 40 int i=0,j=0; 41 while(i<len1) 42 { 43 if(j==-1||str1[i]==str2[j]) 44 { 45 i++; 46 j++; 47 } 48 else 49 { 50 j=Next[j]; 51 } 52 if(j==len2) 53 { 54 ans++; 55 j=Next[j]; 56 // i=i-j+1; //These two lines will time out put line 57 is // J = 0; 58 } 59 } 60 return ANS; 61 is } 62 is 63 is int main () 64 { 65 int T; 66 Scanf ( " % D " , & T); 67 68 the while (T-- ) 69 { 70 Scanf ( " % S% S " , str1, str2); 71 is GetNext (str1); 72 the printf ( " % D \ n-" , KMP (str2, str1)); 73 } 74 return 0 ; 75 }
First slip, after the fill hole