orazma

d.cpp

Mar 30th, 2026
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 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. ll st2[MAXN];
  17.  
  18. ll calc_hash(string s, ll st[], int p, int MOD){
  19.     ll hash1 = 0;
  20.     int len = s.size();
  21.     s = "#" + s;
  22.     for (int i = 1; i <= len; i++){
  23.         hash1 += (s[i] - 'a' + 1) * st[i] % MOD;
  24.         if (hash1 > MOD){
  25.             hash1 -= MOD;
  26.         }
  27.     }
  28.     return hash1;
  29. }
  30. map <pair <int, int>, bool> ma[MAXN];
  31.  
  32. int main(){
  33.     IOS;
  34.     int n, m;
  35.     cin >> n >> m;
  36.     st1[0] = 1;
  37.     st2[0] = 1;
  38.     for (int i = 1; i < MAXN; i++){
  39.         st1[i] = (st1[i - 1] * p1) % MOD1;
  40.         st2[i] = (st2[i - 1] * p2) % MOD2;
  41.     }
  42.     for (int i = 1; i <= n; i++){
  43.         string s;
  44.         cin >> s;
  45.         ll hash1 = calc_hash(s, st1, p1, MOD1);
  46.         ll hash2 = calc_hash(s, st2, p2, MOD2);
  47.         //cout << hash1 << endl;
  48.         ma[int(s.size())][make_pair(hash1, hash2)] = 1;
  49.     }
  50.  
  51.     for (int i = 1; i <= m; i++){
  52.         string s;
  53.         cin >> s;
  54.         ll hash1 = calc_hash(s, st1, p1, MOD1);
  55.         ll hash2 = calc_hash(s, st2, p2, MOD2);
  56.         int len = s.size();
  57.         s = "#" + s;
  58.         bool flag = false;
  59.         for (int j = 1; j <= len; j++){
  60.             for (char c = 'a'; c <= 'c'; c++){
  61.                 if (s[j] == c)
  62.                     continue;
  63.                 ll new_hash1 = (hash1 - (s[j] - 'a' + 1) * st1[j] % MOD1 + MOD1 + (c - 'a' + 1) * st1[j] % MOD1) % MOD1;
  64.                 ll new_hash2 = (hash2 - (s[j] - 'a' + 1) * st2[j] % MOD2 + MOD2 + (c - 'a' + 1) * st2[j] % MOD2) % MOD2;
  65.                 //cout << new_hash1 << endl;
  66.                 if (ma[len].count(make_pair(new_hash1, new_hash2)) != 0){
  67.                     flag = true;
  68.                     break;
  69.                 }
  70.             }
  71.             if (flag){
  72.                 break;
  73.             }
  74.         }
  75.         if (flag){
  76.             cout << "YES\n";
  77.         }
  78.         else{
  79.             cout << "NO\n";
  80.         }
  81.  
  82.     }
  83.  
  84.  
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment