bmhs算法

#include<string.h>
main()
{
        char *text="substering searching algorithm search";
        char *patt="search",* match_text;
        int shift[256];
        int patt_size=strlen(patt);
        int i;
        int text_size,limit,match_size;
        for(i=0;i<256;i++)
                shift[i]=patt_size+1;
        for(i=0;i<patt_size;i++)
                *(shift+(*(patt+i)))=patt_size-i;
        text_size=strlen(text);
        limit=text_size-i+1;
        for(i=0;i<limit;i+=shift[text[i+patt_size] ] )
        {
                if(text[i]==*patt)
                {
                        match_text=text+i+1;
                        match_size=1;
                        do{
                                if(match_size==patt_size)
                                printf("i = %d\n", i);
                        }while( (*match_text++)==patt[match_size++] );
                }
        }
}

posted @ 2013-05-30 15:01  麦峰强  阅读(849)  评论(0编辑  收藏  举报