|
| 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_FiniteAutomatonMatcher |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_FiniteAutomatonMatcher |
| 25 | +#define FUNC_FiniteAutomatonMatcher |
| 26 | + |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +#include "NaiveStringMatcher.cpp" |
| 30 | + |
| 31 | +template <typename T> |
| 32 | +void FiniteAutomatonMatcher(const std::vector<T>& S, |
| 33 | + const std::vector<std::vector<size_t>>& delta, |
| 34 | + T o, std::vector<size_t>& ans) { |
| 35 | + size_t n = S.size(), m = delta.size() - 1; |
| 36 | + size_t q = 0; |
| 37 | + for (size_t i = 0; i < n; i++) { |
| 38 | + q = delta[q][S[i] - o]; |
| 39 | + if (q == m) |
| 40 | + ans.push_back(i - m + 1); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +void ComputeTransitionFunction(const std::vector<T>& P, size_t d, T o, |
| 46 | + std::vector<std::vector<size_t>>& delta) { |
| 47 | + size_t m = P.size(); |
| 48 | + for (size_t q = 0; q <= m; q++) { |
| 49 | + delta.push_back(std::vector<size_t>()); |
| 50 | + for (size_t a = 0; a <= d; a++) { |
| 51 | + size_t k = std::min(m, q + 1); |
| 52 | + while (k && (P[k-1] - o != (T) a || !equal(P, 0, P, q - k+1, k-1))) |
| 53 | + k--; |
| 54 | + delta[q].push_back(k); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
| 60 | +#ifdef MAIN_FiniteAutomatonMatcher |
| 61 | +int main(int argc, char *argv[]) { |
| 62 | + const size_t n = get_argv(argc, argv, 1, 40); |
| 63 | + const size_t m = get_argv(argc, argv, 2, 3); |
| 64 | + const size_t d = get_argv(argc, argv, 3, 2); |
| 65 | + const size_t compute = get_argv(argc, argv, 4, 1); |
| 66 | + std::vector<char> S, P; |
| 67 | + random_integers<char>(S, 'a', 'a' + d, n); |
| 68 | + random_integers<char>(P, 'a', 'a' + d, m); |
| 69 | + output_integers(S, ""); |
| 70 | + output_integers(P, ""); |
| 71 | + std::vector<std::vector<size_t>> delta; |
| 72 | + ComputeTransitionFunction(P, d, 'a', delta); |
| 73 | + std::vector<size_t> ans; |
| 74 | + FiniteAutomatonMatcher(S, delta, 'a', ans); |
| 75 | + output_integers(ans); |
| 76 | + if (compute) { |
| 77 | + std::vector<size_t> ans1; |
| 78 | + NaiveStringMatcher(S, P, ans1); |
| 79 | + output_integers(ans1); |
| 80 | + std::cout << std::boolalpha << (ans == ans1) << std::endl; |
| 81 | + } |
| 82 | + return 0; |
| 83 | +} |
| 84 | +#endif |
| 85 | + |
0 commit comments