Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define ll long long
- #define pb push_back
- #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- using namespace std;
- const int MAXN = 6e5 + 10;
- const int MOD1 = 1e9 + 7;
- const int MOD2 = 1e9 + 9;
- const int p1 = 31;
- const int p2 = 37;
- int n;
- ll st1[MAXN];
- ll st2[MAXN];
- ll calc_hash(string s, ll st[], int p, int MOD){
- ll hash1 = 0;
- int len = s.size();
- s = "#" + s;
- for (int i = 1; i <= len; i++){
- hash1 += (s[i] - 'a' + 1) * st[i] % MOD;
- if (hash1 > MOD){
- hash1 -= MOD;
- }
- }
- return hash1;
- }
- map <pair <int, int>, bool> ma[MAXN];
- int main(){
- IOS;
- int n, m;
- cin >> n >> m;
- st1[0] = 1;
- st2[0] = 1;
- for (int i = 1; i < MAXN; i++){
- st1[i] = (st1[i - 1] * p1) % MOD1;
- st2[i] = (st2[i - 1] * p2) % MOD2;
- }
- for (int i = 1; i <= n; i++){
- string s;
- cin >> s;
- ll hash1 = calc_hash(s, st1, p1, MOD1);
- ll hash2 = calc_hash(s, st2, p2, MOD2);
- //cout << hash1 << endl;
- ma[int(s.size())][make_pair(hash1, hash2)] = 1;
- }
- for (int i = 1; i <= m; i++){
- string s;
- cin >> s;
- ll hash1 = calc_hash(s, st1, p1, MOD1);
- ll hash2 = calc_hash(s, st2, p2, MOD2);
- int len = s.size();
- s = "#" + s;
- bool flag = false;
- for (int j = 1; j <= len; j++){
- for (char c = 'a'; c <= 'c'; c++){
- if (s[j] == c)
- continue;
- ll new_hash1 = (hash1 - (s[j] - 'a' + 1) * st1[j] % MOD1 + MOD1 + (c - 'a' + 1) * st1[j] % MOD1) % MOD1;
- ll new_hash2 = (hash2 - (s[j] - 'a' + 1) * st2[j] % MOD2 + MOD2 + (c - 'a' + 1) * st2[j] % MOD2) % MOD2;
- //cout << new_hash1 << endl;
- if (ma[len].count(make_pair(new_hash1, new_hash2)) != 0){
- flag = true;
- break;
- }
- }
- if (flag){
- break;
- }
- }
- if (flag){
- cout << "YES\n";
- }
- else{
- cout << "NO\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment