/** ____ ____ ____ ____ ____ ____ ||l |||e |||i |||n |||a |||d || ||__|||__|||__|||__|||__|||__|| |/__\|/__\|/__\|/__\|/__\|/__\| **/ #include #define endl '\n' using namespace std; typedef long long ll; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, m, a[30], tt[30]; void solve() { cin >> n >> m; for (int i = 1; i <= m; i ++) { char c; int x; cin >> c >> x; a[c - 'a'] += x; } string s; for (int i = 1; i <= n; i ++) { cin >> s; int sz = s.size(); bool pos = true; for (int j = 0; j < 26; j ++) { int t = 0, cnt = 0; if (s[0] - 'a' == j) t ++, cnt ++; for (int k = 1; k < sz; k ++) { if (s[k] - 'a' == j) { cnt ++; if (s[k - 1] - 'a' != j) { t ++; ///cout << k << endl; } } } ///cout << "t = " << t << endl; if (cnt != a[j]) { pos = false; } tt[j] = t; } for (int j = 0; j < 26; j ++) for (int p = 0; p < 26; p ++) { int first = 0; while(first < sz && s[first] - 'a' != j) first ++; int last = sz - 1; while(last >= 0 && s[last] - 'a' != j) last --; int bef = 0; for (int k = 0; k < first; k ++) if (s[k] - 'a' == p) bef = true; int aft = 0; for (int k = sz - 1; k > last; k --) if (s[k] - 'a' == p) aft = true; int bet = 0; for (int k = first; k <= last; k ++) if (s[k] - 'a' == p) bet = true; if (bet == true && (bef == true || aft == true)) { pos = false; } if (bet == false && (bef != aft) && tt[j] == 2 && tt[p] == 2) { ///cout << j << " " << p << " " << tt[j] << " " << tt[p] << endl; pos = false; } } if (pos) cout << "Yes" << endl; else cout << "No" << endl; } } int main() { freopen ("constructor.in", "r", stdin); freopen ("constructor.out", "w", stdout); solve(); return 0; } /** 5 5 a 2 b 3 c 1 x 2 y 4 aabbbcxxyyyy ycbaabbxxyyy ababbxxyyycy xxbacabbyyy yaabbbcxyyyx */