BF模式匹配算法!字符串的模式匹配。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值