Advertisement
cyberjab

Сам алгоритм

Dec 21st, 2023
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. //#pragma GCC optimize("03")
  2. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include <queue>
  14. #include <deque>
  15. #include <cmath>
  16. #include <numeric>
  17. #include <algorithm>
  18. #include <ctime>
  19. #include <chrono>
  20. #include <random>
  21. #include <functional>
  22.  
  23. using namespace std;
  24. const int MOD = 1e9 + 7;
  25. const int xx = 257;
  26.  
  27. vector<long long> x = {1};
  28.  
  29. bool isequal(int from1, int from2, int len, vector<long long> h, vector<long long> x, vector<long long> h2) {
  30.     return (h[from1 + len - 1] + h2[from2 - 1] * x[len]) % MOD ==
  31.         (h2[from2 + len - 1] + h[from1 - 1] * x[len]) % MOD;
  32. }
  33.  
  34. void make_x_koefs(int n) {
  35.     int k = x.size();
  36.     if (n + 1 > k) {
  37.         x.resize(n + 1);
  38.     }
  39.     for (int i = k; i < n + 1; i++) {
  40.         x[i] = (x[i - 1] * xx) % MOD;
  41.     }
  42. }
  43.  
  44. vector <long long> make_polynom(string s) {
  45.     vector <long long> h(s.size(), 0);
  46.     for (int i = 1; i < s.size(); i++) {
  47.         h[i] = (h[i - 1] * xx + s[i]) % MOD;
  48.     }
  49.     return h;
  50. }
  51.  
  52. int main() {
  53.     ios_base::sync_with_stdio(0);
  54.     cin.tie(0);
  55.     /*cout << setprecision(x)*/
  56.     cout << fixed;    
  57.     string a = " abvsdsf";
  58.     string b = " sds";
  59.     make_x_koefs(a.size());
  60.     vector<long long> h = make_polynom(a);
  61.     vector<long long> h2 = make_polynom(b);
  62.     for (int i = 1; i <= a.size() - b.size() + 1; i++) {
  63.         cout << isequal(i, 1, b.size() - 1, h, x, h2) << " ";
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement