Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. std::vector<int> FunctionZ(std::string& str) {
  9. int len = static_cast<int>(str.length());
  10. std::vector<int> zf(len, 0);
  11. if (!str.empty()) {
  12. zf[0] = len;
  13. }
  14.  
  15. int left = 0;
  16. int right = 0;
  17. for (int ind = 1; ind < len; ++ind) {
  18. if (ind <= right) {
  19. zf[ind] = std::min(right - ind + 1, zf[ind - left]);
  20. }
  21. while (zf[ind] + ind < len && str[zf[ind]] == str[zf[ind] + ind]) {
  22. ++zf[ind];
  23. }
  24. if (ind + zf[ind] - 1 > right) {
  25. // обновляем границы z-блока
  26. left = ind;
  27. right = ind + zf[ind] - 1;
  28. }
  29. }
  30.  
  31. return zf;
  32. }
  33.  
  34. int main() {
  35. std::ifstream input_stream("../fuzzy_matching/input.txt");
  36. std::string pattern;
  37. std::string text;
  38.  
  39. input_stream >> pattern;
  40. input_stream >> text;
  41.  
  42. std::string str = pattern + "#" + text;
  43. auto z1 = FunctionZ(str);
  44.  
  45. std::reverse(pattern.begin(), pattern.end());
  46. std::reverse(text.begin(), text.end());
  47. std::string str_rev = pattern + "#" + text;
  48. auto z2 = FunctionZ(str_rev);
  49.  
  50.  
  51. int shift = pattern.size() + 1;
  52.  
  53. for (int i = 0; i < text.size(); ++i) {
  54. if (z1[shift + i] == pattern.size()) { // 0 ошибок
  55. std::cout << i + 1 << ' ';
  56. } else
  57. if (z1[i + shift] + z2[shift + text.size() - 1 - i] + 1 == pattern.size()) {
  58. std::cout << i + 1 << ' ';
  59. }
  60. }
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement