直接上代码,不懂horspool算法请先百度
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAX 256
typedef 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 ;
}