../ Algorithm Templates
-rw-r--r--aguang1.5 KiB2 min read

modified

KMP Pattern Searching Algorithm

KMP is a pattern searching algorithm. For given searching string ss and pattern string pp, it pre-caculates a partial match table of ss. Through this partial match table, it can find the matching positions in O(∣s∣+∣p∣)O(|s| + |p|) time.

Template

c++
#include<string>
#include<vector>

using namespace std;

class kmp {

  public:
    
    static vector<int> pmt_of(string p) {

      vector<int> pmt = { -1 };

      for (int i = 0, j = -1; i < p.length(); ) {

        if (j == -1 || p[i] == p[j]) {

          ++i, pmt.push_back(++j);
        } else {

          j = pmt[j];
        }
      }

      return pmt;
    }

    static int first_of(string s, string p) {

      vector<int> pmt = pmt_of(p);

      int i = 0, j = 0;

      while (i < s.length() && j < (int) p.length()) {

        if (j == -1 || s[i] == p[j]) {

          ++i, ++j;
        } else {

          j = pmt[j];
        }
      }

      return j == (int) p.length() ? i - j : -1;
    }

    static vector<int> all_of(string s, string p) {

      vector<int> pmt = pmt_of(p), indexes = {};

      int i = 0, j = 0;

      while (i < s.length() && j < (int) p.length()) {

        if (j == -1 || s[i] == p[j]) {

          ++i, ++j;

          if (j == (int) p.length()) {

            indexes.push_back(i - j);

            i = i - j, j = -1;
          }
        } else {

          j = pmt[j];
        }
      }

      return indexes;
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window