C ++ implements the KMP algorithm for String Matching

Source: Internet
Author: User

 


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 ~

 

Related Article

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.