字符串匹配之horspool算法

来源:互联网 发布:陕西网络品牌推广公司 编辑:程序博客网 时间:2024/06/06 03:43

直接上代码,不懂horspool算法请先百度


#include <stdio.h>#include <string.h>#include <unistd.h>#define MAX 256typedef struct{    int arr[64];    int count  ;}info;static int move[MAX] ;void init(const char *str , int n){    int i;    for(i = 0 ; i < MAX ;i++)move[i]=n ;    for(i = 0 ; i < n-1 ;i++)        move[str[i]]=n-i-1 ;}void horspool(const char *str1 ,const char *str2 , info *a){    int i,pos=0;    int len1=strlen(str1),len2=strlen(str2);    init(str2,len2);    while(pos<=len1-len2){        i=len2-1 ;while(i>=0 && str1[i+pos]==str2[i])    --i;if(-1==i){            a->arr[a->count++]=pos;    pos += len2 ;    continue ;}pos+=move[str1[pos+len2-1]];     }    return ;
}



0 0
原创粉丝点击