Codeforces852G(字符串hash)

G. Bathroom terminal

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

 //2017-09-03
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string> using namespace std; const int N = ; char wenhao[] = {'a', 'b', 'c', 'd', 'e'};
int position[], tot, ans, len;
map<string, int> mp, book;
map<string, int>::iterator iter;
char parten[N];
string str; void dfs(int step) {
if (step == len) {
parten[tot] = '\0';
string tmp(parten);
iter = mp.find(tmp);
if (iter != mp.end() && book.find(tmp) == book.end()){
ans += iter->second;
book.insert(make_pair(tmp, ));
}
return;
}
if (str[step] == '?') {
for (int i = ; i < ; i++) {
parten[tot++] = wenhao[i];
dfs(step + );
tot--;
}
dfs(step + );
} else {
parten[tot++] = str[step];
dfs(step + );
tot--;
}
}
int main() {
//freopen("inputG.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie();
int n, m;
while (cin >> n >> m) {
while (n--) {
cin >> str;
mp[str]++;
}
while (m--) {
book.clear();
tot = ;
ans = ;
cin >> str;
len = str.length();
dfs();
cout << ans << endl;
}
mp.clear();
book.clear();
} return ;
}
上一篇:UI学习笔记---第一天


下一篇:android studio 使用 SVN