trafik

A Hashes

Oct 22nd, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define ld long double
  5. #define len(v) (int)v.size()
  6. #define all(v) v.begin(), v.end()
  7. #define rall(v) v.rbegin(), v.rend()
  8. #define pii pair<int, int>
  9. #define vi vector<int>
  10. #define vii vector<vector<int>>
  11. #define vpii vector<pair<int, int>>
  12. #define ull unsigned long long
  13. //#define int long long
  14. //#define ll ull
  15. const int N = 1e6 + 1;
  16. const int maxn = 2e5 + 10;
  17. const int C = 20;
  18. const int logn = 20;
  19. const int inf = 1e9;
  20. const ll mod = 1e9 + 7;
  21. const int M = 1e9;
  22. const ull M2 = 998244353;
  23. const ld eps = 1e-6;
  24. using namespace std;
  25.  
  26. // random
  27. std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count());
  28.  
  29. template<class T>
  30. istream &operator>>(istream &in, vector<T> &a) {
  31.     for (auto &i : a)
  32.         in >> i;
  33.     return in;
  34. }
  35.  
  36. template<class T>
  37. ostream &operator<<(ostream &out, vector<T> &a) {
  38.     for (auto &i : a)
  39.         out << i << ' ';
  40.     return out;
  41. }
  42.  
  43. ll binpow (ll a, ll n) {
  44.     if (n == 0)
  45.         return 1ll;
  46.     if (n % 2 == 1)
  47.         return ((binpow (a, n-1) * a) % mod);
  48.     else {
  49.         ll b = binpow (a, n/2);
  50.         return ((b * b) % mod);
  51.     }
  52. }
  53.  
  54. ll inv(ll x) {
  55.     return binpow(x, 1200720071 - 2);
  56. }
  57.  
  58. int p = 59, m = 1e9 + 33;
  59. ll ppow[maxn];
  60. void clchash(string a, vector<ll>& h, vector<ll>& h2) {
  61.     int n = len(a);
  62.     h.resize(n + 1); h2.resize(n + 1);
  63.     h[0] = 0;
  64.     for (int i = 0; i < n; ++i) { // h[n]
  65.         h[i + 1] = ((h[i] * p) % m + a[i] * 1ll) % m;
  66.     }
  67.     h2[n] = 0;
  68.     for (int i = n - 1; i >= 0; --i) { // h[n]
  69.         h2[i] = ((h2[i + 1] * p) % m + a[i] * 1ll) % m;
  70.     }
  71. }
  72. ll geth(int l, int r, vector<ll>& h) { // [l, r] ; 0 <= l, r < n ;  p^{r - l} * p^l = p^r
  73.     return ((h[r + 1] - (h[l] * ppow[r - l + 1]) % m) % m);
  74. }
  75. ll geth2(int l, int r, vector<ll>& h) {
  76.     return (h[l] - (h[r + 1] * ppow[r - l + 1]) % m) % m;
  77. }
  78.  
  79.  
  80. vector<int> solve(int n, string s) {
  81.     vector<ll> h, h2;
  82.     clchash(s, h, h2);
  83.     vector<int> a(n);
  84.     a[0] = 1;
  85.     for (int i = 1; i < n; ++i) {
  86.         if (i == n - 1) {
  87.             cout << "";
  88.         }
  89.         int l = -1;
  90.         int r = i + 1;
  91.         //cout << i << '\n';
  92.         while (l + 1 < r) {
  93.             int mid = (l + r) / 2;
  94.             ll get1 = geth(0, mid, h);
  95.             ll get2 = geth2(i - mid, i, h2);
  96.             //cout << 0 << ' ' << m << "     " << i - m << ' ' << i << '\n';
  97.             if (get1 == get2)
  98.                 l = mid;
  99.             else
  100.                 r = mid;
  101.         }
  102.         a[i] = l + 1;
  103.     }
  104.     return a;
  105.     /*string a;
  106.     cin >> a;
  107.     vector<ll> h, h2;
  108.     clchash(a, h, h2);
  109.     int k; cin >> k;
  110.     while (k--) {
  111.         int l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2;
  112.         cout << geth(l1, r1, h) << ' ' << geth2(l2, r2, h2) << '\n';
  113.     }
  114.      */
  115. }
  116.  
  117. vector<int> zfunc(string s) {
  118.     int n = len(s);
  119.     vector<int> z(n + 1);
  120.     int l = 0, r = 0;
  121.     for (int i = 1; i < n; ++i) {
  122.         if (r >= i) z[i] = min(z[i - l], r - i + 1);
  123.         while (z[i] + 1 < n && s[z[i]] == s[z[i] + i])
  124.             ++z[i];
  125.         if (i + z[i] - 1 > r) {
  126.             l = i;
  127.             r = i + z[i] - 1;
  128.         }
  129.     }
  130.     return z;
  131. }
  132.  
  133. vector<int> solve2(int n, string s) {
  134.     string ss = s;
  135.     reverse(all(s));
  136.     ss += s;
  137.     vi zt = zfunc(ss);
  138.     vi res;
  139.     for (int i = len(ss) - 1; i >= len(s); --i)
  140.         res.push_back(zt[i]);
  141.     return res;
  142. }
  143.  
  144.  
  145.  
  146. signed main() {
  147.     ios::sync_with_stdio(false);
  148.     cin.tie(nullptr);
  149.     cout.tie(nullptr);
  150.  
  151.     /*ppow[0] = 1ll;
  152.     for (int i = 1; i < maxn; ++i) {
  153.         ppow[i] = (ppow[i - 1] * p) % m;
  154.     }
  155.  
  156.     int test = 1;
  157.     while (true) {
  158.         int n = gen() % 15;
  159.         string s;
  160.         for (int i = 0; i < n; ++i) {
  161.             char cur = ((gen() % 250) + 250) % 250;
  162.             while (!((cur <= 'z' && cur >= 'a') || (cur <= 'Z' && cur >= 'A')))
  163.                 cur = ((gen() % 250) + 250) % 250;
  164.             s += cur;
  165.         }
  166.         cout << "TEST " << test << ": ";
  167.         cout << s << '\n';
  168.         vi ans1 = solve(n, s), ans2 = solve2(n, s);
  169.         for (int i = 0; i < n; ++i) {
  170.             if (ans1[i] != ans2[i]) {
  171.                 cout << "WA" << ' ' << test << '\n';
  172.                 cout << n << ' ' << s << '\n';
  173.                 exit(0);
  174.             }
  175.         }
  176.         test++;
  177.     }
  178.     */
  179.     int T = 1;
  180.     //cin >> T;
  181.     while (T--) {
  182.         solve();
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment