0
点赞
收藏
分享

微信扫一扫

BF算法

霸姨 2023-02-06 阅读 45


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;
}


举报

相关推荐

0 条评论