Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 52a18b1

Browse files
committed
KMPMatcher.cpp
1 parent e4b44f5 commit 52a18b1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

KMPMatcher.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// algorithm - some algorithms in "Introduction to Algorithms", third edition
3+
// Copyright (C) 2018 lxylxy123456
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef MAIN
20+
#define MAIN
21+
#define MAIN_KMPMatcher
22+
#endif
23+
24+
#ifndef FUNC_KMPMatcher
25+
#define FUNC_KMPMatcher
26+
27+
#include "utils.h"
28+
29+
#include "NaiveStringMatcher.cpp"
30+
31+
template <typename T>
32+
std::vector<size_t> ComputePrefixFunction(const std::vector<T>& P) {
33+
size_t m = P.size();
34+
std::vector<size_t> pi;
35+
pi.push_back(0);
36+
pi.push_back(0);
37+
size_t k = 0;
38+
for (size_t q = 2; q <= m; q++) {
39+
while (k && P[k] != P[q - 1])
40+
k = pi[k];
41+
if (P[k] == P[q - 1])
42+
k++;
43+
pi.push_back(k);
44+
}
45+
return pi;
46+
}
47+
48+
template <typename T>
49+
void KMPMatcher(const std::vector<T>& S, const std::vector<T>& P,
50+
std::vector<size_t>& ans) {
51+
size_t n = S.size(), m = P.size();
52+
std::vector<size_t> pi = ComputePrefixFunction(P);
53+
size_t q = 0;
54+
for (size_t i = 0; i < n; i++) {
55+
while (q && P[q] != S[i])
56+
q = pi[q];
57+
if (P[q] == S[i])
58+
q++;
59+
if (q == m) {
60+
ans.push_back(i - m + 1);
61+
q = pi[q];
62+
}
63+
}
64+
}
65+
#endif
66+
67+
#ifdef MAIN_KMPMatcher
68+
int main(int argc, char *argv[]) {
69+
const size_t n = get_argv(argc, argv, 1, 40);
70+
const size_t m = get_argv(argc, argv, 2, 3);
71+
const size_t d = get_argv(argc, argv, 3, 2);
72+
const size_t compute = get_argv(argc, argv, 4, 1);
73+
std::vector<char> S, P;
74+
random_integers<char>(S, 'a', 'a' + d, n);
75+
random_integers<char>(P, 'a', 'a' + d, m);
76+
output_integers(S, "");
77+
output_integers(P, "");
78+
std::vector<size_t> ans;
79+
KMPMatcher(S, P, ans);
80+
output_integers(ans);
81+
if (compute) {
82+
std::vector<size_t> ans1;
83+
NaiveStringMatcher(S, P, ans1);
84+
output_integers(ans1);
85+
std::cout << std::boolalpha << (ans == ans1) << std::endl;
86+
}
87+
return 0;
88+
}
89+
#endif
90+

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@
209209
| 32 | RabinKarpMatcher.cpp | Rabin Karp Matcher | 993 |
210210
| 32 | FiniteAutomatonMatcher.cpp | Finite Automaton Matcher | 999 |
211211
| 32 | FiniteAutomatonMatcher.cpp | Compute Transition Function | 1001 |
212+
| 32 | KMPMatcher.cpp | KMP Matcher | 1005 |
213+
| 32 | KMPMatcher.cpp | Compute Prefix Function | 1006 |
212214

213215
# Supplementary Files
214216
* `utils.h`: Utils

0 commit comments

Comments
 (0)