Data Structure _ string_string_kmp algorithm _ C ++ implementation

Source: Internet
Author: User

"Head. h"

 

 

View plaincopy to clipboardprint? # Include <iostream>
# Include <string>
Using namespace std;
 
Class STRING
{
Public:
Void GetString ();
Void GetSubString ();
Void KMP ();
Void Print ();
Private:
Void GetNext ();
String str;
String sub;
Int next [1000];
Int strlen;
Int sublen;
};
 
Void STRING: GetString ()
{
Cout <"Please Input The MainString:" <endl;
Cin> str;
Strlen = str. length ();
}
 
Void STRING: GetSubString ()
{
Cout <"Please Input The SubString:" <endl;
Cin> sub;
Sublen = sub. length ();
}
 
Void STRING: KMP ()
{
Cout <"KMP Method Called! "<Endl;
GetNext ();
Int I, j;
I = j = 0;
While (I <strlen & j <sublen)
{
If (j =-1 | str [I] = sub [j])
{
I ++;
J ++;
}
Else
{
J = next [j];
}
}
If (j = sublen)
{
Cout <"Succeed! Position = "<I-j + 1 <endl;
}
Else
{
Cout <"Failed! "<Endl;
}
}
 
Void STRING: Print ()
{
Cout <"Main String =" <str <endl <"Length =" <strlen <endl;
Cout <"SubString =" <sub <endl <"Length =" <sublen <endl;
}
 
Void STRING: GetNext ()
{
Int I, j;
Next [0] =-1;
I = 0; j =-1;
While (I <sublen-1)
{
If (j =-1 | sub [I] = sub [j])
{
I ++;
J ++;
If (sub [I]! = Sub [j])
Next [I] = j;
Else
Next [I] = next [j];
}
Else
{
J = next [j];
 
}
}
}
# Include <iostream>
# Include <string>
Using namespace std;

Class STRING
{
Public:
Void GetString ();
Void GetSubString ();
Void KMP ();
Void Print ();
Private:
Void GetNext ();
String str;
String sub;
Int next [1000];
Int strlen;
Int sublen;
};

Void STRING: GetString ()
{
Cout <"Please Input The MainString:" <endl;
Cin> str;
Strlen = str. length ();
}

Void STRING: GetSubString ()
{
Cout <"Please Input The SubString:" <endl;
Cin> sub;
Sublen = sub. length ();
}

Void STRING: KMP ()
{
Cout <"KMP Method Called! "<Endl;
GetNext ();
Int I, j;
I = j = 0;
While (I <strlen & j <sublen)
{
If (j =-1 | str [I] = sub [j])
{
I ++;
J ++;
}
Else
{
J = next [j];
}
}
If (j = sublen)
{
Cout <"Succeed! Position = "<I-j + 1 <endl;
}
Else
{
Cout <"Failed! "<Endl;
}
}

Void STRING: Print ()
{
Cout <"Main String =" <str <endl <"Length =" <strlen <endl;
Cout <"SubString =" <sub <endl <"Length =" <sublen <endl;
}

Void STRING: GetNext ()
{
Int I, j;
Next [0] =-1;
I = 0; j =-1;
While (I <sublen-1)
{
If (j =-1 | sub [I] = sub [j])
{
I ++;
J ++;
If (sub [I]! = Sub [j])
Next [I] = j;
Else
Next [I] = next [j];
}
Else
{
J = next [j];

}
}
}

 

 


"Main. cpp"

 

 

 


View plaincopy to clipboardprint? # Include <iostream>
# Include "head. h"
Using namespace std;
 
Int main ()
{
STRING str;
Char choice;
While (1)
{
Cout <"Your Choice Please? "<Endl
<"1. Set Main String" <endl
<"2. Set SubString" <endl
<"3. KMP" <endl
<"4. Print" <endl
<"5. Quit" <endl;
Cin> choice;
Switch (choice)
{
Case '1 ':
Str. GetString ();
Break;
Case '2 ':
Str. GetSubString ();
Break;
Case '3 ':
Str. KMP ();
Break;
Case '4 ':
Str. Print ();
Break;
Case '5 ':
Return 0;
Default:
Cout <"No Such Choice! "<Endl;
Break;
}
}
}

Author: "Dreamer Thinker Doer"
 

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.