adfasdfadsfasdf

Untitled

Mar 10th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <unordered_set>
  4. #include <unordered_map>
  5.  
  6. using namespace std;
  7. typedef unsigned long long ull;
  8.  
  9. const int PRIME_CONST = 29;
  10. const ull MAX_SIZE = INT_MAX;
  11.  
  12. ull scratch_hash(string s) {
  13.     ull hash = 0;
  14.     for (short i = 0; i < s.length(); i++) {
  15.        hash = (PRIME_CONST * hash + s[i]) % MAX_SIZE;
  16.     }
  17.     return hash;
  18. }
  19.  
  20. int ground_truth(string latin, int len) {
  21.     hash<string> hasher;
  22.     unordered_set<size_t> mp;
  23.     for (short i = 0; i < len - 1; i += 1) {
  24.         string removed = latin.substr(0, i) + latin.substr(i + 2, len - 1);
  25.         mp.insert(hasher(removed));
  26.     }
  27.     return mp.size();
  28. }
  29.  
  30. void solve() {
  31.     int len;
  32.     string latin;
  33.     cin >> len >> latin;
  34.  
  35.     unordered_set<ull> mp;
  36.     ull prefix_hash = 0;
  37.     ull postfix_hash = 0;
  38.     ull old_bit_base = 1;
  39.  
  40.     for (int i = 0; i < len - 3; i += 1) {
  41.         old_bit_base = (old_bit_base * PRIME_CONST) % MAX_SIZE;
  42.     }
  43.  
  44.  
  45.     for (short i = 2; i < len; i += 1) {
  46.         postfix_hash = (postfix_hash * PRIME_CONST + latin[i]) % MAX_SIZE;
  47.     }
  48.  
  49.     ull hash;
  50.     for (short i = 0; i < len - 1; i += 1) {
  51.         string prefix_string = latin.substr(0, i);
  52.         string postfix_string = latin.substr(i + 2, len - 1);
  53.         string removed = prefix_string + postfix_string;
  54.  
  55.         hash = (prefix_hash + postfix_hash) % MAX_SIZE;
  56.  
  57.         cout << "On string: " << removed << " hash " << hash << " expected " << scratch_hash(removed) << endl;
  58.         cout << "Prefix : " << prefix_hash << " Postfix: " << postfix_hash << endl;
  59.         mp.insert(hash);
  60.         prefix_hash = (prefix_hash + latin[i] * old_bit_base) % MAX_SIZE;
  61.         postfix_hash = (postfix_hash - latin[i+2] * old_bit_base) % MAX_SIZE;
  62.         old_bit_base = (old_bit_base / PRIME_CONST) % MAX_SIZE;
  63.     }
  64.     cout << mp.size() << endl;
  65. }
  66.  
  67. int main() {
  68.     int n = 0;
  69.     cin >> n;
  70.     while (n--) {
  71.         solve();
  72.     }
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment