Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. const int INT_INF = static_cast<const int>(1e9);
  10. const int MOD = static_cast<const int>(1e9 + 7);
  11. const int MOD1 = static_cast<const int>(1e9 + 9);
  12. const int INITI = 1000000;
  13.  
  14. vector<pair<int, int>> p_pow;
  15.  
  16. int p = 31;
  17.  
  18. struct Hash
  19. {
  20.     size_t n;
  21.     vector<pair<int, int>> hash;
  22.  
  23.     Hash()
  24.     {
  25.         n = 0;
  26.     }
  27.  
  28.     explicit Hash(string s)
  29.     {
  30.         n = s.size();
  31.         hash.resize(n);
  32.         for (int i = 0; i < n; i++) {
  33.             if (i > 0) {
  34.                 hash[i].first = static_cast<int>(1ll * hash[i - 1].first * p % MOD);
  35.             } else {
  36.                 hash[i].first = 0;
  37.             }
  38.             hash[i].first += 1ll * (s[i] - 'a' + 1) % MOD;
  39.             hash[i].first %= MOD;
  40.             if (i > 0) {
  41.                 hash[i].second = static_cast<int>(1ll * hash[i - 1].second * p % MOD1);
  42.             } else {
  43.                 hash[i].second = 0;
  44.             }
  45.             hash[i].second += 1ll * (s[i] - 'a' + 1) % MOD1;
  46.             hash[i].second %= MOD1;
  47.  
  48.             if (hash[i].first < 0) {
  49.                 hash[i].first += MOD;
  50.             }
  51.             if (hash[i].second < 0) {
  52.                 hash[i].second += MOD1;
  53.             }
  54.         }
  55.     }
  56.  
  57.     pair<int, int> get(int l, int r)
  58.     {
  59.         int len = r - l + 1;
  60.         int h1 = hash[r].first;
  61.         if (l != 0) {
  62.             h1 -= 1ll * hash[l - 1].first * p_pow[len].first % MOD;
  63.         }
  64.         h1 %= MOD;
  65.         if (h1 < 0) {
  66.             h1 += MOD;
  67.         }
  68.  
  69.         int h2 = hash[r].second;
  70.         if (l != 0) {
  71.             h2 -= 1ll * hash[l - 1].second * p_pow[len].second % MOD1;
  72.         }
  73.         h2 %= MOD1;
  74.         if (h2 < 0) {
  75.             h2 += MOD1;
  76.         }
  77.         return make_pair(h1, h2);
  78.     }
  79. };
  80.  
  81.  
  82. int main()
  83. {
  84.     ios_base::sync_with_stdio(false);
  85.     cin.tie();
  86. #ifdef LOCAL
  87.     freopen("input.txt", "r", stdin);
  88.     freopen("output.txt", "w", stdout);
  89. #endif
  90.     p_pow.resize(INITI, make_pair(0, 0));
  91.     p_pow[0].first = 1;
  92.     p_pow[0].second = 1;
  93.     for (int i = 1; i < p_pow.size(); i++) {
  94.         p_pow[i].first = static_cast<int>((1ll * p_pow[i - 1].first * p) % MOD);
  95.         p_pow[i].second = static_cast<int>((1ll * p_pow[i - 1].second * p) % MOD1);
  96.     }
  97.     size_t n;
  98.     cin >> n;
  99.     vector<Hash> hash(n);
  100.     vector<string> g(n);
  101.     int mn = INT_INF;
  102.     for (int i = 0; i < n; i++) {
  103.         cin >> g[i];
  104.         if (g[i].size() < mn) {
  105.             mn = g[i].size();
  106.         }
  107.         hash[i] = Hash(g[i]);
  108.     }
  109.     int l = 1, r = mn;
  110.     pair<int, pair<int, int>> ans = {-1, {-1, -1}};
  111.     while (l <= r) {
  112.         int len = (l + r) / 2;
  113.         map<pair<int, int>, pair<int, pair<int, int>>> res;
  114.         for (int k = 0; k < n; k++) {
  115.             map<pair<int, int>, int> prev;
  116.             for (int i = 0; i <= hash[k].n - len; i++) {
  117.                 prev[hash[k].get(i, i + len - 1)] = i;
  118.             }
  119.             for (auto& x : prev) {
  120.                 res[x.first] = {res[x.first].first + 1, {k, x.second}};
  121.             }
  122.         }
  123.         pair<int, int> cur = {-1, -1};
  124.         for (auto& x : res) {
  125.             if (x.second.first == n) {
  126.                 cur = x.second.second;
  127.                 break;
  128.             }
  129.         }
  130.         if (cur.first != -1) {
  131.             ans = {len, cur};
  132.             l = len + 1;
  133.         } else {
  134.             r = len - 1;
  135.         }
  136.     }
  137.     cout << g[ans.second.first].substr(static_cast<unsigned int>(ans.second.second),
  138.                                        static_cast<unsigned int>(ans.first));
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement