Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <iostream>
- #include <algorithm>
- std::vector<int> FunctionZ(std::string& str) {
- int len = static_cast<int>(str.length());
- std::vector<int> zf(len, 0);
- if (!str.empty()) {
- zf[0] = len;
- }
- int left = 0;
- int right = 0;
- for (int ind = 1; ind < len; ++ind) {
- if (ind <= right) {
- zf[ind] = std::min(right - ind + 1, zf[ind - left]);
- }
- while (zf[ind] + ind < len && str[zf[ind]] == str[zf[ind] + ind]) {
- ++zf[ind];
- }
- if (ind + zf[ind] - 1 > right) {
- // обновляем границы z-блока
- left = ind;
- right = ind + zf[ind] - 1;
- }
- }
- return zf;
- }
- int main() {
- std::ifstream input_stream("../fuzzy_matching/input.txt");
- std::string pattern;
- std::string text;
- input_stream >> pattern;
- input_stream >> text;
- std::string str = pattern + "#" + text;
- auto z1 = FunctionZ(str);
- std::reverse(pattern.begin(), pattern.end());
- std::reverse(text.begin(), text.end());
- std::string str_rev = pattern + "#" + text;
- auto z2 = FunctionZ(str_rev);
- int shift = pattern.size() + 1;
- for (int i = 0; i < text.size(); ++i) {
- if (z1[shift + i] == pattern.size()) { // 0 ошибок
- std::cout << i + 1 << ' ';
- } else
- if (z1[i + shift] + z2[shift + text.size() - 1 - i] + 1 == pattern.size()) {
- std::cout << i + 1 << ' ';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement