fork(2) download
  1. /*
  2.   Problem "1517. Freedom of Choice" from acm.timus.ru
  3.  
  4.   Solution: polynomial hash, sort, binary search, O(n log(n)^2)
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <cassert>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <random>
  12. #include <chrono>
  13. #include <string>
  14.  
  15. typedef unsigned long long ull;
  16.  
  17. // Generate random base in (before, after) open interval:
  18. int gen_base(const int before, const int after) {
  19. auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
  20. std::mt19937 mt_rand(seed);
  21. int base = std::uniform_int_distribution<int>(before+1, after)(mt_rand);
  22. return base % 2 == 0 ? base-1 : base;
  23. }
  24.  
  25. struct PolyHash {
  26. // -------- Static variables --------
  27. static const int mod = (int)1e9+123; // prime mod of polynomial hashing
  28. static std::vector<int> pow1; // powers of base modulo mod
  29. static std::vector<ull> pow2; // powers of base modulo 2^64
  30. static int base; // base (point of hashing)
  31.  
  32. // --------- Static functons --------
  33. static inline int diff(int a, int b) {
  34. // Diff between `a` and `b` modulo mod (0 <= a < mod, 0 <= b < mod)
  35. return (a -= b) < 0 ? a + mod : a;
  36. }
  37.  
  38. // -------------- Variables of class -------------
  39. std::vector<int> pref1; // Hash on prefix modulo mod
  40. std::vector<ull> pref2; // Hash on prefix modulo 2^64
  41.  
  42. // Cunstructor from string:
  43. PolyHash(const std::string& s)
  44. : pref1(s.size()+1u, 0)
  45. , pref2(s.size()+1u, 0)
  46. {
  47. assert(base < mod);
  48. const int n = s.size(); // Firstly calculated needed power of base:
  49. while ((int)pow1.size() <= n) {
  50. pow1.push_back(1LL * pow1.back() * base % mod);
  51. pow2.push_back(pow2.back() * base);
  52. }
  53. for (int i = 0; i < n; ++i) { // Fill arrays with polynomial hashes on prefix
  54. assert(base > s[i]);
  55. pref1[i+1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
  56. pref2[i+1] = pref2[i] + s[i] * pow2[i];
  57. }
  58. }
  59.  
  60. // Polynomial hash of subsequence [pos, pos+len)
  61. // If mxPow != 0, value automatically multiply on base in needed power. Finally base ^ mxPow
  62. inline std::pair<int, ull> operator()(const int pos, const int len, const int mxPow = 0) const {
  63. int hash1 = pref1[pos+len] - pref1[pos];
  64. ull hash2 = pref2[pos+len] - pref2[pos];
  65. if (hash1 < 0) hash1 += mod;
  66. if (mxPow != 0) {
  67. hash1 = 1LL * hash1 * pow1[mxPow-(pos+len-1)] % mod;
  68. hash2 *= pow2[mxPow-(pos+len-1)];
  69. }
  70. return std::make_pair(hash1, hash2);
  71. }
  72. };
  73.  
  74. // Init static variables of PolyHash class:
  75. int PolyHash::base((int)1e9+7);
  76. std::vector<int> PolyHash::pow1{1};
  77. std::vector<ull> PolyHash::pow2{1};
  78.  
  79. int main() {
  80. // Input:
  81. int n;
  82. scanf("%d", &n);
  83. char buf[1+100000];
  84. scanf("%100000s", buf);
  85. std::string a(buf);
  86. scanf("%100000s", buf);
  87. std::string b(buf);
  88.  
  89. // Calculate max neede power of base:
  90. const int mxPow = std::max((int)a.size(), (int)b.size());
  91.  
  92. // Gen random base of hashing:
  93. PolyHash::base = gen_base(256, PolyHash::mod);
  94.  
  95. // Create hashing objects from strings:
  96. PolyHash hash_a(a), hash_b(b);
  97.  
  98. // Binary search by length of same subsequence:
  99. int pos = -1, low = 0, high = std::min(a.size(), b.size())+1;
  100. while (high - low > 1) {
  101. int mid = (low + high) / 2;
  102. std::vector<std::pair<int,ull>> hashes;
  103. for (int i = 0; i + mid <= n; ++i) {
  104. hashes.push_back(hash_a(i,mid,mxPow));
  105. }
  106. std::sort(hashes.begin(), hashes.end());
  107. int p = -1;
  108. for (int i = 0; i + mid <= n; ++i) {
  109. if (std::binary_search(hashes.begin(), hashes.end(), hash_b(i, mid, mxPow))) {
  110. p = i;
  111. break;
  112. }
  113. }
  114. if (p >= 0) {
  115. low = mid;
  116. pos = p;
  117. } else {
  118. high = mid;
  119. }
  120. }
  121. assert(pos >= 0);
  122. // Output answer:
  123. printf("%s", b.substr(pos, low).c_str());
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0s 4488KB
stdin
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
stdout
THEGREATALBANIA