It is easy to understand and can be implemented in C ++ according to his ideas. The Code is as follows:
[Cpp]
Include <iostream>
# Include <string>
Using namespace std;
// Calculate the partial matching value of a time. For example, if str = "ABCDAB", 2 is returned.
Int single_match (string str)
{
Int n = str. length ();
String * prefix = new string [n-1] ();
String * suffix = new string [n-1] ();
For (int I = 0; I! = N-1; ++ I ){
For (int j = 0; j <= I; ++ j)
Prefix [I] + = str [j];
For (int k = I + 1; k! = N; ++ k)
Suffix [I] + = str [k];
}
Int match_num = 0;
For (int I = 0; I <n-1; ++ I)
For (int j = 0; j <n-1; ++ j)
If (prefix [I] = suffix [j])
Match_num + = prefix [I]. length ();
Return match_num;
}
// Calculate part of the matching table for the entire string
Void partial_match_table (string str, int * table)
{
Int n = str. length ();
For (int I = 0; I! = N; ++ I ){
String sub_str;
For (int j = 0; j <= I; ++ j)
Sub_str + = str [j];
Int temp = single_match (sub_str );
Table [I] = temp;
}
}
// KMP Algorithm
Int Knuth_Morris_Pratt (string str1, string str2, int * table)
{
Int n1 = str1.length ();
Int n2 = str2.length ();
Int I = 0;
While (I <n1-n2 ){
Int j = 0;
While (j <n2 ){
If (str1 [I + j] = str2 [j])
++ J;
Else
Break;
}
If (j = n2)
Break;
Else if (j = 0)
++ I;
Else
I + = j-table [J-1];
}
If (I> n1-n2)
Return-1;
Return I;
}
Int main ()
{
String str1 ("bbc abcdab abcdabcdabde ");
String str2 ("ABCDABD ");
Int n = str2.length ();
Int * table = new int [n];
For (int I = 0; I <n; ++ I)
Cout <table [I] <'';
Cout <endl;
Cout <Knuth_Morris_Pratt (str1, str2, table) <endl;
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
// Calculate the partial matching value of a time. For example, if str = "ABCDAB", 2 is returned.
Int single_match (string str)
{
Int n = str. length ();
String * prefix = new string [n-1] ();
String * suffix = new string [n-1] ();
For (int I = 0; I! = N-1; ++ I ){
For (int j = 0; j <= I; ++ j)
Prefix [I] + = str [j];
For (int k = I + 1; k! = N; ++ k)
Suffix [I] + = str [k];
}
Int match_num = 0;
For (int I = 0; I <n-1; ++ I)
For (int j = 0; j <n-1; ++ j)
If (prefix [I] = suffix [j])
Match_num + = prefix [I]. length ();
Return match_num;
}
// Calculate part of the matching table for the entire string
Void partial_match_table (string str, int * table)
{
Int n = str. length ();
For (int I = 0; I! = N; ++ I ){
String sub_str;
For (int j = 0; j <= I; ++ j)
Sub_str + = str [j];
Int temp = single_match (sub_str );
Table [I] = temp;
}
}
// KMP Algorithm
Int Knuth_Morris_Pratt (string str1, string str2, int * table)
{
Int n1 = str1.length ();
Int n2 = str2.length ();
Int I = 0;
While (I <n1-n2 ){
Int j = 0;
While (j <n2 ){
If (str1 [I + j] = str2 [j])
++ J;
Else
Break;
}
If (j = n2)
Break;
Else if (j = 0)
++ I;
Else
I + = j-table [J-1];
}
If (I> n1-n2)
Return-1;
Return I;
}
Int main ()
{
String str1 ("bbc abcdab abcdabcdabde ");
String str2 ("ABCDABD ");
Int n = str2.length ();
Int * table = new int [n];
For (int I = 0; I <n; ++ I)
Cout <table [I] <'';
Cout <endl;
Cout <Knuth_Morris_Pratt (str1, str2, table) <endl;
Return 0;
}
The disadvantage of the Code is that the processing of strings is too stiff. When python is used to the string-type slicing operation, it is too unfamiliar with the use of the string type in C ++, so that the operations to retrieve substrings are implemented by traversing and adding them ......
You are welcome to propose amendments ~