orazma

d.cpp

May 14th, 2026
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MOD1 = 1e9 + 7;
  13. const int MOD2 = 1e9 + 9;
  14. const int MAXN = 1e4 + 10;
  15. int p1 = 31;
  16. int p2 = 37, n;
  17. ll hash1[12][MAXN], hash2[12][MAXN], st1[MAXN], st2[MAXN], len[12];
  18. string s[12];
  19.  
  20. set <pair <ll, ll> > set_hashs;
  21. map <pair <ll, ll>, int > cnt_hashs;
  22.  
  23. pair <ll, ll> best_hash = {-1LL, -1LL};
  24.  
  25. pair <ll, ll> get_hash(int i, int l, int r){
  26.     ll h1 = (hash1[i][r] - hash1[i][l - 1] + MOD1) * st1[10000 - l] % MOD1;
  27.     ll h2 = (hash2[i][r] - hash2[i][l - 1] + MOD2) * st2[10000 - l] % MOD2;
  28.     return {h1, h2};
  29. }
  30. bool check(int d){
  31.     cnt_hashs.clear();
  32.     for (int i = 1; i <= n; i++){
  33.         set_hashs.clear();
  34.         for (int j = d; j <= len[i]; j++){
  35.             set_hashs.insert(get_hash(i, j - d + 1, j));
  36.         }
  37.         for (auto z : set_hashs){
  38.             cnt_hashs[z]++;
  39.             if (cnt_hashs[z] == n){
  40.                 best_hash = z;
  41.                 return true;
  42.             }
  43.         }
  44.     }
  45.     return false;
  46. }
  47.  
  48. void solve(){
  49.     cin >> n;
  50.     int l = 1;
  51.     int r = 10000;
  52.     for (int i = 1; i <= n; i++){
  53.         cin >> s[i];
  54.         r = min(r, int(s[i].size()));
  55.     }
  56.     st1[0] = st2[0] = 1;
  57.     for (int i = 1; i <= 10000; i++){
  58.         st1[i] = st1[i - 1] * p1 % MOD1;
  59.         st2[i] = st2[i - 1] * p2 % MOD2;
  60.     }
  61.     for (int i = 1; i <= n; i++){
  62.         len[i] = s[i].size();
  63.         s[i] = "#" + s[i];
  64.         for (int j = 1; j <= len[i]; j++){
  65.             hash1[i][j] = (hash1[i][j - 1] + (s[i][j] - 'a' + 1) * st1[j]) % MOD1;
  66.             hash2[i][j] = (hash2[i][j - 1] + (s[i][j] - 'a' + 1) * st2[j]) % MOD2;
  67.         }
  68.     }
  69.  
  70.     int ans = 0;
  71.  
  72.     while (l <= r){
  73.         int mid = (l + r) / 2;
  74.         if (check(mid)){
  75.             ans = mid;
  76.             l = mid + 1;
  77.         }
  78.         else{
  79.             r = mid - 1;
  80.         }
  81.     }
  82.  
  83.     check(ans);
  84.     for (int j = ans; j <= len[1]; j++){
  85.         if (get_hash(1, j - ans + 1, j) == best_hash){
  86.             cout << s[1].substr(j - ans + 1, ans);
  87.             return;
  88.         }
  89.     }
  90.     cout << ans;
  91.  
  92. }
  93.  
  94. int main() {
  95.     IOS;
  96.     int t = 1;
  97.     //cin >> t;
  98.     while(t--){
  99.         solve();
  100.     }
  101.     return 0;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment