Problem Description
实现串的BF模式匹配算法,统计在匹配过程中总的字符比较次数,当主串剩余部分不足子串长度时,停止比较。
Input
输入包含两行,第一行为主串s,第二行为子串t。
Output
输出包含两行,第一行为子串在主串中的位置,如果失配,返回0值;第二行为匹配过程中总的字符比较次数。
Sample Input
abacd
ac
Sample Output
3
5
#include<stdio.h>
#include<string.h>
int n=0;
struct ch{
char S[100],T[100];
}ch;
int Index(char s[],char t[],int a,int b){
int i=0,j=0,z=0,c,d;
c=a-1;
d=b-1;
for(;i<=c&&j<=d;i++){
if(s[z]==t[j]){
j++;
z++;
n++;
}
else{
z=z-j+1;
j=0;
n++;
}
}
if(j>d)
return z-j+1;
else return(0);
}
int main(){
int b,c,d;
struct ch a;
gets(a.S);
gets(a.T);
b=strlen(a.S);
c=strlen(a.T);
d=Index(a.S,a.T,b,c);
printf("%d\n",d);
printf("%d",n);
return 0;
}