#include<stdio.h>
#include<string.h>
#define MAXSIZE 1000
struct str{
char data[MAXSIZE];
int len;
};
int main(){
struct str s,t;
int len1,len2;
int i=0,j=0,k=0;
len1=len2=0;
char dt1[MAXSIZE],dt2[MAXSIZE];
//初始化字符串s到结构体s.data中
printf("请输入字符串s:");
scanf("%s",dt1);
len1=strlen(dt1);
strcpy(s.data,dt1);
s.len=len1;
//初始化字符串t到结构体t.data中
printf("请输入字符串t:");
scanf("%s",dt2);
len2=strlen(dt2);
strcpy(t.data,dt2);
t.len=len2;
//找子字符串
while(j<t.len&&i<s.len){
if(s.data[i]==t.data[j]){
i++;j++;
}else{
k++;i=k;j=0;
}
}
if(j>=t.len){
printf("找到了%d\n",k+1);
}if(k>=s.len){
printf("not find");
}
}
关于数据结构BF算法/bf算法找子字符串的代码
最新推荐文章于 2024-07-22 20:42:52 发布