Algorithm: pattern searching

kmp算法:用一个数组保存了上一个需要开始搜索的index,比如AAACAAA就是0, 1, 2, 0, 1, 2, 3, ABCABC就是0, 0, 0, 1, 2, 3,复杂度O(M+N)

 1 #include <iostream>
 2 #include <map>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string pattern = "ABCABC";
12     string s = "ABCABCABCABCBCABC";
13     vector<int> lps(pattern.size());
14     int len = 0;
15     int i = 1;
16     lps[0] = 0;
17     while (i < pattern.size()) {
18         if (pattern[i] == pattern[len]) {
19             len++;
20             lps[i] = len;
21             i++;
22         }
23         else if (len != 0) len = lps[len-1];
24         else {
25             lps[i] = 0;
26             i++;
27         }
28     }
29     //for (int i = 0; i < pattern.size(); i++) cout << lps[i] << endl;
30     i = 0;
31     int j = 0;
32     while (i < s.size()) {
33         if (s[i] == pattern[j]) {
34             j++;
35             i++;
36         }
37         if (j == pattern.size()) {
38             cout << i - j << endl;
39             j = lps[j-1];
40         }
41         else if (pattern[j] != s[i]) {
42             if (j != 0) j = lps[j-1];
43             else i++;
44         }
45     }
46     return 0;
47 }

 robin-karp算法,给pattern做hash-value,给s前pattern.size()的子串也做hash-value,如果相同则输出当前位置,如果不相同则去掉这个子串的第一个,加上新进来的那个字符。复杂度最好是O(M+N),最差O(M*N)。

http://www.geeksforgeeks.org/searching-for-patterns-set-3-rabin-karp-algorithm/

 FA算法:http://www.geeksforgeeks.org/searching-for-patterns-set-5-finite-automata/

 Boyer Moore Algorithm - bad character heuristic: 记录各个字母在pattern最后出现的位置,从后往前比较,如果不匹配,就向前移动s当前字母在pattern的位置与当前匹配到的位置的差值,如果这个位置在当前位置后,则只能往前移一个。复杂度最好是O(N/M),最坏是O(N*M)。

http://www.geeksforgeeks.org/pattern-searching-set-7-boyer-moore-algorithm-bad-character-heuristic/

 

转载于:https://www.cnblogs.com/yingzhongwen/p/3556876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值