|
| 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 | + |
0 commit comments