Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <cstring>
- using namespace std;
- typedef long long T;
- #define N 100100
- const T P = 173, MOD = 100000000000031ll;
- T pows[N];
- set < T > hashes;
- inline T next_hash(T hash, char last, char next, int p)
- {
- hash -= (last * pows[p]) % MOD;
- if (hash < 0)
- hash += MOD;
- return (hash * P + next) % MOD;
- }
- string s, sr;
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w+", stdout);
- s = sr = "";
- getline(cin, s);
- cout << s << endl;
- for (int i = s.length() - 1; i >= 0; --i)
- if (s[i] == '1')
- sr += '0';
- else
- sr += '1';
- cout << sr << endl;
- pows[0] = 1;
- for (int i = 0; i < N; pows[i + 1] = (P * pows[i]) % MOD, ++i);
- cout << pows[1] << " " << pows[2] << " " << pows[2] / pows[1] << endl;
- int L = 0, R = s.length() >> 1, m;
- while (L + 1 < R)
- {
- cout << "L = " << L << " R = " << R << endl;
- m = (L + R) >> 1;
- T hs = 0;
- for (int i = 0; i < m; ++i)
- hs = (hs * P + s[i]) % MOD;
- hashes.clear();
- hashes.insert(hs);
- for (int i = m, ln = s.length(); i < ln; ++i)
- hashes.insert(hs = next_hash(hs, s[i - m], s[i], m - 1));
- hs = 0;
- for (int i = 0; i < m; ++i)
- hs = (hs * P + sr[i]) % MOD;
- if (hashes.find(hs) != hashes.end())
- {
- L = m;
- continue;
- }
- for (int i = m, ln = sr.length(); i < ln; ++i)
- if (hashes.find(hs = next_hash(hs, sr[i - m], sr[i], m - 1)) != hashes.end())
- {
- L = m;
- break;
- }
- if (hashes.find(hs) == hashes.end())
- R = m;
- }
- cout << L;
- return 0;
- }
Add Comment
Please, Sign In to add comment