数据结构_串_串的_KMP算法_C++实现

2014-11-24 12:47:58 · 作者: · 浏览: 1

"head.h"

view plaincopy to clipboardprint #include
#include
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 :"< cin>>str;
strlen=str.length();
}

void STRING::GetSubString()
{
cout<<"Please Input The SubString :"< cin>>sub;
sublen=sub.length();
}

void STRING::KMP()
{
cout<<"KMP Method Called !"< GetNext();
int i,j;
i=j=0;
while(i {
if(j==-1||str[i]==sub[j])
{
i++;
j++;
}
else
{
j=next[j];
}
}
if(j==sublen)
{
cout<<"Succeed ! Position = "< }
else
{
cout<<"Failed !"< }
}

void STRING::Print()
{
cout<<"Main String = "< cin>>str;
strlen=str.length();
}

void STRING::GetSubString()
{
cout<<"Please Input The SubString :"< cin>>sub;
sublen=sub.length();
}

void STRING::KMP()
{
cout<<"KMP Method Called !"< GetNext();
int i,j;
i=j=0;
while(i {
if(j==-1||str[i]==sub[j])
{
i++;
j++;
}
else
{
j=next[j];
}
}
if(j==sublen)
{
cout<<"Succeed ! Position = "< }
else
{
cout<<"Failed !"< }
}

void STRING::Print()
{
cout<<"Main String = "<

}
}
}


"main.cpp"


view plaincopy to clipboardprint #include
#include"head.h"
using namespace std;

int main()
{
STRING str;
char choice;
while(1)
{
cout<<"Your Choice Please "< <<"1 . Set Main String"< <<"2 . Set SubString"< <<"3 . KMP"< <<"4 . Print"< <<"5 . Quit"< 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 !"< break;
}
}
}

作者“Dreamer Thinker Doer”