字符串匹配算法
先打个酱油
socket通信过程如下:
建立套接字->绑定端口->开始listen->client端connect->client端send数据->server端accept->client端recieve->close连接
1.Knuth-Morris-Pratt算法
这个算法是不是大家都已经了然于胸了呢,很有效率,但人的智慧是无穷尽的,经典与权威是用来被挑战的!
让字符串滑得更远点,滑得更快点吧!
2.Boyer-Moore算法
模式与目标的比较从右往左而不是从左往右,性能上的改进令人吃惊!
3.Sunday算法:刚才我向大家推荐BM算法时,某兄立马说此法比Boyer-Moore更快匹配,未加验证,先收藏。
//Sunday.h : Sunday算法。 |
02 |
//************************************************************************* |
03 |
//Copyright: |
04 |
//Author: Sail |
05 |
//Filename: Sunday string match |
06 |
//Last Mod time: |
07 |
//************************************************************************* |
08 |
//Remarks: Sunday算法是比较快速的字符串匹配算法,比KMP和Booyer-moore算法都 |
09 |
//快,这三种算法都需要提前进行预处理,Booyer-moore是反向比对字符串,Kmp和 |
10 |
//Sunday则是正向比对,各自的预处理方法都不同。 |
11 |
//Sunday算法主要思想是让指向主串的指针在匹配过程中,跳过尽可能多的长度, |
12 |
//再和模式串进行匹配。平均状况下时间复杂度:O(主串长度/模式串长度)。 |
13 |
//************************************************************************* |
14 |
#ifndef _my_sunday |
15 |
#define _my_sunday |
16 |
17 |
#include <string.h> |
18 |
#include <vector> |
19 |
20 |
using std::vector; |
21 |
22 |
inline void sunday(const char* ori ,const char* pat, vector<int> * res) |
23 |
{ |
24 |
int i=0; |
25 |
int j=0; |
26 |
int olen=strlen(ori); |
27 |
int plen=strlen(pat); |
28 |
const int max_size=255; |
29 |
int * next =new int[max_size]; |
30 |
for (i=0;i<max_size;++i) |
31 |
{ |
32 |
next[i]=plen+1; |
33 |
} |
34 |
for (i=0;i<plen;++i) |
35 |
{ |
36 |
next[pat[i]]=plen-i; |
37 |
} |
38 |
i=0; |
39 |
while(i<=olen-plen) |
40 |
{ |
41 |
while(j<plen) |
42 |
{ |
43 |
if (ori[i+j]!=pat[j]) |
44 |
{ |
45 |
break; |
46 |
} |
47 |
++j; |
48 |
} |
49 |
if (j==plen) |
50 |
{ |
51 |
(*res).push_back(i); |
52 |
j=0; |
53 |
} |
54 |
i+=next[ori[i+plen]]; |
55 |
} |
56 |
} |
57 |
#endif |
思想的高度决定事情的成败!
浙公网安备 33010602011771号