zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    ubuntu 1804 docker install
    windows shortcut
    gallary
    g++ play
    linux profile
    terminator
    tmux
    ubuntu18
    windows toolkit
    windows terminal CLI
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/3556876.html
Copyright © 2011-2022 走看看