Bathroom terminal

Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message “Play Me”. He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is connected to the terminal next to him. After pressing a Play button a rough voice starts playing from the tape:

“Listen up Smith. As you can see, you are in pretty tough situation and in order to escape, you have to solve a puzzle.

You are given N strings which represent words. Each word is of the maximum length L and consists of characters ‘a’-‘e’. You are also given M strings which represent patterns. Pattern is a string of length  ≤  L and consists of characters ‘a’-‘e’ as well as the maximum 3 characters ‘?’. Character ‘?’ is an unknown character, meaning it can be equal to any character ‘a’-‘e’, or even an empty character. For each pattern find the number of words that matches with the given pattern. After solving it and typing the result in the terminal, the key will drop from the ceiling and you may escape. Let the game begin.”

Help Smith escape.

Input

The first line of input contains two integers N and M (1 ≤ N ≤  100 000, 1 ≤ M ≤  5000), representing the number of words and patterns respectively.

The next N lines represent each word, and after those N lines, following M lines represent each pattern. Each word and each pattern has a maximum length L (1 ≤ L ≤ 50). Each pattern has no more that three characters ‘?’. All other characters in words and patters are lowercase English letters from ‘a’ to ‘e’.

Output

Output contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.

Example

input

3 1
abc
aec
ac
a?c

output

3

Note

If we switch ‘?’ with ‘b’, ‘e’ and with empty character, we get ‘abc’, ‘aec’ and ‘ac’ respectively.

Solution:

#include <bits/stdc++.h>

using namespace std;

int main() {
int n, m;
cin >> n >> m;
map <string, int> mp;
for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
mp[s]++;
}
for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
vector <int> q;
for (int j = 0; j < (int) s.length(); j++) {
      if (s[j] == '?') {
        q.push_back(j);
      }
    }
    set <string> was;
int ans = 0;
for (char a = 'a'; a <= 'f'; a++) {
      if (q.size() < 1 && a != 'f') {
        continue;
      }
      for (char b = 'a'; b <= 'f'; b++) {
        if (q.size() < 2 && b != 'f') {
          continue;
        }
        for (char c = 'a'; c <= 'f'; c++) {
          if (q.size() < 3 && c != 'f') {
            continue;
          }
          string st = s;
          if (q.size() >= 1) {
st[q[0]] = a;
}
if (q.size() >= 2) {
st[q[1]] = b;
}
if (q.size() >= 3) {
st[q[2]] = c;
}
while (st.find('f') != string::npos) {
st.erase(st.begin() + st.find('f'));
}
if (was.find(st) != was.end()) {
continue;
}
if (mp.find(st) != mp.end()) {
was.insert(st);
ans += mp[st];
}
}
}
}
printf("%d\n", ans);
}
return 0;
}