Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- using namespace std;
- typedef long long ll;
- const int INT_INF = static_cast<const int>(1e9);
- const int MOD = static_cast<const int>(1e9 + 7);
- const int MOD1 = static_cast<const int>(1e9 + 9);
- const int INITI = 1000000;
- vector<pair<int, int>> p_pow;
- int p = 31;
- struct Hash
- {
- size_t n;
- vector<pair<int, int>> hash;
- Hash()
- {
- n = 0;
- }
- explicit Hash(string s)
- {
- n = s.size();
- hash.resize(n);
- for (int i = 0; i < n; i++) {
- if (i > 0) {
- hash[i].first = static_cast<int>(1ll * hash[i - 1].first * p % MOD);
- } else {
- hash[i].first = 0;
- }
- hash[i].first += 1ll * (s[i] - 'a' + 1) % MOD;
- hash[i].first %= MOD;
- if (i > 0) {
- hash[i].second = static_cast<int>(1ll * hash[i - 1].second * p % MOD1);
- } else {
- hash[i].second = 0;
- }
- hash[i].second += 1ll * (s[i] - 'a' + 1) % MOD1;
- hash[i].second %= MOD1;
- if (hash[i].first < 0) {
- hash[i].first += MOD;
- }
- if (hash[i].second < 0) {
- hash[i].second += MOD1;
- }
- }
- }
- pair<int, int> get(int l, int r)
- {
- int len = r - l + 1;
- int h1 = hash[r].first;
- if (l != 0) {
- h1 -= 1ll * hash[l - 1].first * p_pow[len].first % MOD;
- }
- h1 %= MOD;
- if (h1 < 0) {
- h1 += MOD;
- }
- int h2 = hash[r].second;
- if (l != 0) {
- h2 -= 1ll * hash[l - 1].second * p_pow[len].second % MOD1;
- }
- h2 %= MOD1;
- if (h2 < 0) {
- h2 += MOD1;
- }
- return make_pair(h1, h2);
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie();
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- p_pow.resize(INITI, make_pair(0, 0));
- p_pow[0].first = 1;
- p_pow[0].second = 1;
- for (int i = 1; i < p_pow.size(); i++) {
- p_pow[i].first = static_cast<int>((1ll * p_pow[i - 1].first * p) % MOD);
- p_pow[i].second = static_cast<int>((1ll * p_pow[i - 1].second * p) % MOD1);
- }
- size_t n;
- cin >> n;
- vector<Hash> hash(n);
- vector<string> g(n);
- int mn = INT_INF;
- for (int i = 0; i < n; i++) {
- cin >> g[i];
- if (g[i].size() < mn) {
- mn = g[i].size();
- }
- hash[i] = Hash(g[i]);
- }
- int l = 1, r = mn;
- pair<int, pair<int, int>> ans = {-1, {-1, -1}};
- while (l <= r) {
- int len = (l + r) / 2;
- map<pair<int, int>, pair<int, pair<int, int>>> res;
- for (int k = 0; k < n; k++) {
- map<pair<int, int>, int> prev;
- for (int i = 0; i <= hash[k].n - len; i++) {
- prev[hash[k].get(i, i + len - 1)] = i;
- }
- for (auto& x : prev) {
- res[x.first] = {res[x.first].first + 1, {k, x.second}};
- }
- }
- pair<int, int> cur = {-1, -1};
- for (auto& x : res) {
- if (x.second.first == n) {
- cur = x.second.second;
- break;
- }
- }
- if (cur.first != -1) {
- ans = {len, cur};
- l = len + 1;
- } else {
- r = len - 1;
- }
- }
- cout << g[ans.second.first].substr(static_cast<unsigned int>(ans.second.second),
- static_cast<unsigned int>(ans.first));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement