MCPcopy Create free account
hub / github.com/acm-clan/algorithm-stone / kmp_matcher

Function kmp_matcher

templates/kmp.cpp:36–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36int kmp_matcher(std::string& t, std::string& p)
37{
38 auto prefix = compute_prefix_function(p);
39
40 int j = 0;
41 int k = 0;
42
43 while (j < t.length() && k < p.length()) {
44 if (k == -1 || t[j] == p[k]) {
45 // 相同则前进
46 k++;
47 j++;
48 } else {
49 // 不同则回退尝试
50 k = prefix[k];
51 }
52 }
53
54 if (k == p.length()) {
55 return j - p.length();
56 }
57 return -1;
58}
59
60void print(std::vector<int> v)
61{

Callers 1

mainFunction · 0.85

Calls 1

compute_prefix_functionFunction · 0.70

Tested by

no test coverage detected