BF:
t–>模式串
s–>目标串
是否在s中可以找到t,从头开始匹配
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
/*
BF算法--串的匹配
*/
int BF(char s[],char t[]){
int i=0,j=0;
while(i<strlen(s) && j<strlen(t)){
if(s[i]==t[j]){
i++;
j++;
}else{
i = i-j+1;
j = 0;
}
}
if(j>=strlen(t)){
return (i-strlen(t));
}else{
return -1;
}
}
int main(){
char s[]="aaaaab";
char t[]="aaab";
int cnt = BF(s,t);
printf("%d\n",cnt);
return 0;
}