码迷,mamicode.com
首页 > 编程语言 > 详细

Sunday串匹配算法 C语言实现

时间:2014-12-19 13:04:57      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

 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语言实现

标签:

原文地址:http://www.cnblogs.com/jimaojin/p/4173638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!