orazma

d.cpp

Mar 30th, 2026
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define ll long long
  5. #define pb push_back
  6. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  7.  
  8. using namespace std;
  9. const int MAXN = 6e5 + 10;
  10. const int MOD1 = 1e9 + 7;
  11. const int MOD2 = 1e9 + 9;
  12. const int p1 = 31;
  13. const int p2 = 37;
  14. int n;
  15. ll st1[MAXN];
  16.  
  17. ll calc_hash(string s, ll st[], int p, int MOD){
  18.     ll hash1 = 0;
  19.     int len = s.size();
  20.     s = "#" + s;
  21.     for (int i = 1; i <= len; i++){
  22.         hash1 += (s[i] - 'a' + 1) * st[i] % MOD;
  23.         if (hash1 > MOD){
  24.             hash1 -= MOD;
  25.         }
  26.     }
  27.     return hash1;
  28. }
  29. map <int, bool> ma[MAXN];
  30.  
  31. int main(){
  32.     IOS;
  33.     int n, m;
  34.     cin >> n >> m;
  35.     st1[0] = 1;
  36.     for (int i = 1; i < MAXN; i++){
  37.         st1[i] = (st1[i - 1] * p1) % MOD1;
  38.     }
  39.     for (int i = 1; i <= n; i++){
  40.         string s;
  41.         cin >> s;
  42.         ll hash1 = calc_hash(s, st1, p1, MOD1);
  43.         cout << hash1 << endl;
  44.         ma[int(s.size())][hash1] = 1;
  45.     }
  46.  
  47.     for (int i = 1; i <= m; i++){
  48.         string s;
  49.         cin >> s;
  50.         ll hash1 = calc_hash(s, st1, p1, MOD1);
  51.         int len = s.size();
  52.         s = "#" + s;
  53.         bool flag = false;
  54.         for (int j = 1; j <= len; j++){
  55.             for (char c = 'a'; c <= 'c'; c++){
  56.                 if (s[j] == c)
  57.                     continue;
  58.                 hash1 = (hash1 - (s[j] - 'a' + 1) * st1[j] % MOD1 + MOD1 + (c - 'a' + 1) * st1[j] % MOD1) % MOD1;
  59.                 cout << hash1 << endl;
  60.                 if (ma[len].count(hash1) != 0){
  61.                     flag = true;
  62.                     break;
  63.                 }
  64.             }
  65.             if (flag){
  66.                 break;
  67.             }
  68.         }
  69.         if (flag){
  70.             cout << "YES\n";
  71.         }
  72.         else{
  73.             cout << "NO\n";
  74.         }
  75.  
  76.     }
  77.  
  78.  
  79. }
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment