| 34 | } |
| 35 | |
| 36 | int 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 | |
| 60 | void print(std::vector<int> v) |
| 61 | { |
no test coverage detected