Advertisement
milon34

String Matching using double hashing(and more problem)

Feb 22nd, 2023 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. problem link::https://cses.fi/problemset/task/1753/
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6. #define ll long long int
  7. const int mx = 1e6 + 5;
  8.  
  9. int pw1[mx + 5], pw2[mx + 5], invPw1[mx + 5], invPw2[mx + 5];
  10.  
  11. inline void normal(ll &a, ll MOD) {
  12.     a %= MOD;
  13.     (a < 0) && (a += MOD);
  14. }
  15.  
  16. inline ll modMul(ll a, ll b, ll MOD) {
  17.     a %= MOD, b %= MOD;
  18.     normal(a, MOD), normal(b, MOD);
  19.     return (a * b) % MOD;
  20. }
  21.  
  22. inline ll modPow(ll b, ll p, ll MOD) {
  23.     ll r = 1;
  24.     while (p) {
  25.         if (p & 1)
  26.             r = modMul(r, b, MOD);
  27.         b = modMul(b, b, MOD);
  28.         p >>= 1;
  29.     }
  30.     return r;
  31. }
  32.  
  33. inline ll modInverse(ll a, ll MOD) { return modPow(a, MOD - 2, MOD); }
  34.  
  35. int p1 = 31, p2 = 37, MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
  36.  
  37. void pw_cal() {
  38.     pw1[0] = 1;
  39.     for (int i = 1; i <= mx; i++) {
  40.         pw1[i] = 1LL * pw1[i - 1] * p1 % MOD1;
  41.     }
  42.     pw2[0] = 1;
  43.     for (int i = 1; i <= mx; i++) {
  44.         pw2[i] = 1LL * pw2[i - 1] * p2 % MOD2;
  45.     }
  46.     int invp1 = modInverse(p1, MOD1);
  47.     int invp2 = modInverse(p2, MOD2);
  48.     invPw1[0] = 1, invPw2[0] = 1;
  49.     for (int i = 1; i <= mx; i++) {
  50.         invPw1[i] = 1LL * invPw1[i - 1] * invp1 % MOD1;
  51.     }
  52.     for (int i = 1; i <= mx; i++) {
  53.         invPw2[i] = 1LL * invPw2[i - 1] * invp2 % MOD2;
  54.     }
  55. }
  56.  
  57. pair<int, int> string_hash(string s) {
  58.     int res1 = 0;
  59.     for (int i = 0; i < s.size(); i++) {
  60.         res1 += 1LL * (s[i] - 'a' + 1) * pw1[i] % MOD1;
  61.         res1 %= MOD1;
  62.     }
  63.     int res2 = 0;
  64.     for (int i = 0; i < s.size(); i++) {
  65.         res2 += 1LL * (s[i] - 'a' + 1) * pw2[i] % MOD2;
  66.         res2 %= MOD2;
  67.     }
  68.     return {res1, res2};
  69. }
  70.  
  71. int pre1[mx + 5], pre2[mx + 5];
  72.  
  73. void build(string s) {
  74.     int n = s.size();
  75.     for (int i = 0; i < n; i++) {
  76.         pre1[i] = 1LL * (s[i] - 'a' + 1) * pw1[i] % MOD1;
  77.         pre2[i] = 1LL * (s[i] - 'a' + 1) * pw2[i] % MOD2;
  78.         if (i)pre1[i] = (pre1[i] + pre1[i - 1]) % MOD1;
  79.         if (i)pre2[i] = (pre2[i] + pre2[i - 1]) % MOD2;
  80.     }
  81. }
  82.  
  83. pair<int, int> getHash(int i, int j) {
  84.     assert(i <= j);
  85.     int hs1 = pre1[j];
  86.     if (i)hs1 = (hs1 - pre1[i - 1] + MOD1) % MOD1;
  87.     hs1 = 1LL * hs1 * invPw1[i] % MOD1;
  88.     int hs2 = pre2[j];
  89.     if (i)hs2 = (hs2 - pre2[i - 1] + MOD2) % MOD2;
  90.     hs2 = 1LL * hs2 * invPw2[i] % MOD2;
  91.     return {hs1, hs2};
  92. }
  93.  
  94. int main() {
  95.     ios_base::sync_with_stdio(false);
  96.     cin.tie(0);
  97.     pw_cal();
  98.     string s1, s2;
  99.     cin >> s1 >> s2;
  100.     build(s1);
  101.     int ans = 0;
  102.     int m = s2.size();
  103.     pair<int, int> check = string_hash(s2);
  104.     for (int i = 0; i + m - 1 < s1.size(); i++) {
  105.         ans += (getHash(i, i + m - 1) == check);
  106.     }
  107.     cout << ans << '\n';
  108.     return 0;
  109. }
  110.  
  111.  
  112.  
  113. //// problem:emon ekta string thakbe jekhane find korte hobe koto gula substring diye oi string generate kora jay.
  114. abababab->ab
  115.  
  116.  
  117.  
  118. int main() {
  119.     ios_base::sync_with_stdio(false);
  120.     cin.tie(0);
  121.     pw_cal();
  122.     string s1;
  123.     cin >> s1;
  124.     build(s1);
  125.     int n = s1.size();
  126.     int cnt = 1;
  127.     for (int len = 1; len <= n / 2; len++) {
  128.         bool ok = true;
  129.         for (int i = 0; i + len - 1 < n; i += len) {
  130.             ok &= getHash(i, i + len - 1) == getHash(0, len - 1);
  131.         }
  132.         if (ok) {
  133.             cnt++;
  134.         }
  135.     }
  136.     cout << cnt << endl;
  137.     return 0;
  138. }
  139.  
  140.  
  141. ////  problem:find the largest string that occurs more than k times:
  142. abbccbbc->length=3.->(bbc)->k=2
  143.  
  144.  
  145. int n;
  146.  
  147. int check(int m) {
  148.     map<pair<int, int>, int> mp;
  149.     for (int i = 0; i + m - 1 < n; i++) {
  150.         mp[getHash(i, i + m - 1)]++;
  151.     }
  152.     int mx = 0;
  153.     for (auto[x, y]:mp) {
  154.         mx = max(mx, y);
  155.     }
  156.     return mx;
  157. }
  158.  
  159. int main() {
  160.     ios_base::sync_with_stdio(false);
  161.     cin.tie(0);
  162.     pw_cal();
  163.     string s;
  164.     cin >> s;
  165.     build(s);
  166.     n = s.size();
  167.     int k;
  168.     cin >> k;
  169.     int left = 1, right = n;
  170.     int res = -1;
  171.     while (left <= right) {
  172.         int md = (left + right) >> 1;
  173.         if (check(md) >= k) {
  174.             left = md + 1;
  175.             res = md;
  176.         } else {
  177.             right = md - 1;
  178.         }
  179.     }
  180.     cout << res << endl;
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement