首页 > 代码库 > Sunday串匹配算法 C语言实现

Sunday串匹配算法 C语言实现

 1 unsigned char * sunday( void * a_buf1, 2                         unsigned int len1, 3                         void * a_buf2, 4                         unsigned int len2 ){ 5  6     unsigned char * buf1 = ( unsigned char * )a_buf1; 7     unsigned char * buf2 = ( unsigned char * )a_buf2; 8  9     unsigned int next[256];10     unsigned int i, j, pos;11 12 13     for( i = 0; i < 256; ++i ){14 15         next[i] = len2 + 1;16     }17 18     for( i = 0; i < len2; ++i ){19 20         next[buf2[i]] = len2 - i;21     }22 23 24     pos = 0;25 26     while( pos < len1 - len2 + 1 ){27 28         i = pos;29         j = 0;30 31         while( j < len2 ){32 33             if( buf1[i] != buf2[j] ){34 35                 pos += next[buf1[pos + len2]];36                 break;37             }38 39             ++i;40             ++j;41         }42 43         if( j == len2 ){44 45             return &buf1[pos];46         }47     }48 49 50     return NULL;51 }                    

 

和KMP一样,在查询小串的时候都不如优化过的库函数strstr。

Sunday串匹配算法 C语言实现