共计 644 个字符,预计需要花费 2 分钟才能阅读完成。
include<stdio.h>
include<stdlib.h>
include<stdbool.h>
define MAXSTRLEN 255
// 存储构造
typedef unsigned char SString[MAXSTRLEN + 1];
// 奢侈匹配模式
int Index(SString S, SString T, int pos){
int i = pos;
int j = 1;
while(i<S[0] && j<=T[0]){if(S[i] == T[j]){
++i;
++j;
}else{
i = i-j+2;
j = 1;
}
}
if(j>T[0]){return i-T[0];
}
return 0;
}
bool StrAssign(SString str, char *src){
char * c = src;
for(int i=0; *c!='\0'; ++i, ++c){if(0==i){str[0] = 0;
}else{str[0] = 0;
for(int j=1; j<=i; j++){str[j] = src[j-1];
str[0] +=1;
}
}
}
}
void main(){
SString S;
SString T;
char a[] = "abbacdefg";
char b[] = "acd";
// 给串调配元素顺便就义掉第一个地位来记录串的理论长度
StrAssign(S, a);
StrAssign(T, b);
int pos = Index(S, T, 3);
if (pos != 0)
printf("模式匹配胜利,找到地位为 %d\n", pos);
else
printf("模式匹配失败");
return 0;
}
正文完