#include
#include
#include
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
#define MAXSTRLEN 255
typedef char SString[MAXSTRLEN+1];
Status StrAssign(SString T, char *chars) {
int i;
if (strlen(chars) > MAXSTRLEN)
return ERROR;
else {
T[0] = strlen(chars);
for (i = 1; i <= T[0]; i++)
T[i] = *(chars + i - 1);
return OK;
}
}
void get_next(SString T, int next[]){
int i = 1, j = 0;
next[1] = 0;
while (i < T[0])
if (j == 0 || T[i] == T[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
for(i=1;i<strlen(T);i++)
printf("j=%d next[%d]=%d \n",i,i,next[i]);
}
void get_nextval(SString T, int nextval[]){
int i = 1, j = 0;
nextval[1] = 0;
while (i < T[0])
if (j == 0 || T[i] == T[j])
{
++i;
++j;
if (T[i] != T[j])
nextval[i] = j;
else
nextval[i] = nextval[j];
} else
j = nextval[j];
for(i=1;i<strlen(T);i++)
printf("j=%d nextval[%d]=%d \n",i,i,nextval[i]);
}
void BF(char s[100],char p[50],int t){
int space,a,b,k=0,num=0;
int pos, i, j, flag;
j = flag = 0;
if(strlen(s)<strlen(p)){
printf("Error:子串长度大于父串.\n");
return ;
}
if(strlen(s)-strlen(p)<t){
printf("Error:匹配位置不合适.\n");
return ;
}
space=i=pos=t;
printf("\n");
while(i < strlen(s))
{
if(j==0&&s[i]!=p[j])
num++;
pos=i;
if(p[j] == s[i]){
while(j < strlen(p))
{
if(p[j] != s[i])
{
k++;
printf("%s %d 此次匹配失败\n",s,k);
for(a=space;a>0;a--)
printf(" ");
for(b=0;b<=j;b++)
printf("%c",p[b]);
printf("\n");
break;
}
else{
k++;
printf("%s %d 此次匹配成功\n",s,k);
for(a=space;a>0;a--)
printf(" ");
for(b=0;b<=j;b++)
printf("%c",p[b]);
printf("\n");
i++;
j++;
}
}
if(j == strlen(p))
flag = 1;
}
else{
k++;
printf("%s %d 此次匹配失败\n",s,k);
for(a=space;a>0;a--)
printf(" ");
printf("%c\n",p[j]) ;
}
space++;
if(flag == 1){
printf("匹配位置为:%d\n", pos);
break;
}
i = ++pos;
j = 0;
if(i > strlen(s) - strlen(p)){
printf("主串中不存在此子串!\n");
break;
}
}
printf("共匹配%d次\n",k);
printf("单个字符匹配次数为%d\n",num);
}
int KMP(SString S, SString T, char* s, char* p, int pos, int next[]){
if(strlen(s)<strlen(p)){
printf("Error:子串长度大于父串.\n");
return -1;
}
if(strlen(s)-strlen(p)<pos){
printf("Error:匹配位置不合适.\n");
return -1;
}
int i = pos, j = 1,k=0,a,b,space=0;
while (i <= S[0] && j <= T[0])
{
if (j == 0 || S[i] == T[j])
{
i++;
j++;
if(j == strlen(T))
{
printf("共循环%d次\n",k);
break;
}
++k;
printf("%s %d next[%d]=%d \n",s,k,j,next[j]);
for(a=space;a>0;a--)
printf(" ");
for(b=1;b<=j;b++)
printf("%c",T[b]);
printf("\n");
if(j==1&&S[i]!=T[j]) simple++;
}
else j = next[j];
space=i-j;
}
printf("单字符循环%d次\n",simple);
if (j > T[0])
return i - T[0];
else
return -1;
}
int main(){
printf("1.输入主串、子串和匹配起始位置\n2.BF算法\n3.KMP算法\n4.KMP改进算法\n0.退出\n");
int t = -1,place;
SString S;
SString T;
int *next,*nextval;
char s[50], p[50];
while(t)
{
printf("请输入:");
scanf("%d",&t);
switch(t)
{
case 0:
break;
case 1:
printf("请输入主串、子串和匹配起始位置:\n");
scanf("%s%s%d",s,p,&place);
StrAssign(S,s) ;
StrAssign(T,p) ;
break;
case 2:
BF(s,p,place);
break;
case 3:
next = new int[T[0]+1];
get_next(T,next);
printf("匹配的位置为 %d\n",KMP(S,T,s,p,place,next));
break;
case 4:
nextval = new int[T[0]+1];
get_nextval(T,nextval) ;
printf("匹配的位置为 %d\n",KMP(S,T,s,p,place,nextval));
break;
default:
break;
}
}
return 0;
}