Guest User

Untitled

a guest
Dec 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <set>
  6. #include <map>
  7. #include <algorithm>
  8. #include <cstring>
  9. using namespace std;
  10. typedef long long T;
  11. #define N 100100
  12.  
  13. const T P = 173, MOD = 100000000000031ll;
  14. T pows[N];
  15. set < T > hashes;
  16.  
  17. inline T next_hash(T hash, char last, char next, int p)
  18. {
  19.     hash -= (last * pows[p]) % MOD;
  20.     if (hash < 0)
  21.         hash += MOD;
  22.     return (hash * P + next) % MOD;
  23. }
  24.  
  25. string s, sr;
  26.  
  27.  
  28. int main()
  29. {
  30.     freopen("input.txt", "r", stdin);
  31.     freopen("output.txt", "w+", stdout);
  32.     s = sr = "";
  33.     getline(cin, s);
  34.     cout << s << endl;
  35.     for (int i = s.length() - 1; i >= 0; --i)
  36.         if (s[i] == '1')
  37.             sr += '0';
  38.         else
  39.             sr += '1';
  40.     cout << sr << endl;
  41.     pows[0] = 1;
  42.     for (int i = 0; i < N; pows[i + 1] = (P * pows[i]) % MOD, ++i);
  43.     cout << pows[1] << " " << pows[2] << " " << pows[2] / pows[1] << endl;
  44.     int L = 0, R = s.length() >> 1, m;
  45.     while (L + 1 < R)
  46.     {
  47.         cout << "L = " << L << " R = " << R << endl;
  48.         m = (L + R) >> 1;
  49.         T hs = 0;
  50.         for (int i = 0; i < m; ++i)
  51.             hs = (hs * P + s[i]) % MOD;
  52.         hashes.clear();
  53.         hashes.insert(hs);
  54.         for (int i = m, ln = s.length(); i < ln; ++i)
  55.             hashes.insert(hs = next_hash(hs, s[i - m], s[i], m - 1));
  56.         hs = 0;
  57.         for (int i = 0; i < m; ++i)
  58.             hs = (hs * P + sr[i]) % MOD;
  59.         if (hashes.find(hs) != hashes.end())
  60.         {
  61.             L = m;
  62.             continue;
  63.         }
  64.         for (int i = m, ln = sr.length(); i < ln; ++i)
  65.             if (hashes.find(hs = next_hash(hs, sr[i - m], sr[i], m - 1)) != hashes.end())
  66.                 {
  67.                     L = m;
  68.                     break;
  69.                 }
  70.         if (hashes.find(hs) == hashes.end())
  71.             R = m;
  72.     }
  73.     cout << L;
  74.     return 0;
  75. }
Add Comment
Please, Sign In to add comment