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

  1. 3 1
    abc
    aec
    ac
    a?c

output

  1. 3

Note

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

  1. //2017-09-03
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <map>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. const int N = ;
  12.  
  13. char wenhao[] = {'a', 'b', 'c', 'd', 'e'};
  14. int position[], tot, ans, len;
  15. map<string, int> mp, book;
  16. map<string, int>::iterator iter;
  17. char parten[N];
  18. string str;
  19.  
  20. void dfs(int step) {
  21. if (step == len) {
  22. parten[tot] = '\0';
  23. string tmp(parten);
  24. iter = mp.find(tmp);
  25. if (iter != mp.end() && book.find(tmp) == book.end()){
  26. ans += iter->second;
  27. book.insert(make_pair(tmp, ));
  28. }
  29. return;
  30. }
  31. if (str[step] == '?') {
  32. for (int i = ; i < ; i++) {
  33. parten[tot++] = wenhao[i];
  34. dfs(step + );
  35. tot--;
  36. }
  37. dfs(step + );
  38. } else {
  39. parten[tot++] = str[step];
  40. dfs(step + );
  41. tot--;
  42. }
  43. }
  44. int main() {
  45. //freopen("inputG.txt", "r", stdin);
  46. ios::sync_with_stdio(false);
  47. cin.tie();
  48. int n, m;
  49. while (cin >> n >> m) {
  50. while (n--) {
  51. cin >> str;
  52. mp[str]++;
  53. }
  54. while (m--) {
  55. book.clear();
  56. tot = ;
  57. ans = ;
  58. cin >> str;
  59. len = str.length();
  60. dfs();
  61. cout << ans << endl;
  62. }
  63. mp.clear();
  64. book.clear();
  65. }
  66.  
  67. return ;
  68. }

Codeforces852G(字符串hash)的更多相关文章

  1. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  2. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...

  3. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  4. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  5. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  6. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  7. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  8. 长度有限制的字符串hash函数

    长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...

  9. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

随机推荐

  1. 【062新题】OCP 12c 062出现大量新题-15

    choose one In your Oracle 12c database, you plan to execute the command: SQL> CREATE TABLESPACE t ...

  2. 移动一根火柴使等式成立js版本

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  3. postgresql-pgbench(转)

    pgbench测试:   pg9.6.2的pgbench报错: [thunisoft@localhost ~]$ pgbench -S -c 8 -t 60 pgbenchdb Segmentatio ...

  4. Dell R730服务器 Raid5配置

    Dell R730服务器,有7块5t硬盘,默认做的RAID5.我们的目的是取其中6块硬盘做RAID5,留一块硬盘做热备. 一块SSD系统盘. 在这里,我具体解释一下 ①6块硬盘做成RAID5 ②6块硬 ...

  5. Jenkins配置自动打包 -- 遇到的坑

    1.把gradle路径设为本地路径 Jenkins部署在远程linux服务器上,使用git将代码下载到服务器路径下后,无法使用gradle命令 因为默认配置都是gradle同步时 实时从网上下载,进入 ...

  6. centos clamav杀毒软件安装配置及查杀,没想到linux下病毒比windows还多!

    centos clamav杀毒软件安装配置及查杀,没想到linux下病毒比windows还多! 一.手动安装 1.下载(官网)    cd /soft     wget http://www.clam ...

  7. Spark中的常用算子

    更多有用的例子和算子讲解参见: http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html map是对每个元素操作, ma ...

  8. 一、Linq简介

    语言集成查询Language Integrated Query(LINQ)是一系列将查询功能集成到C#语言的技术统称. 传统数据查询的缺点: 简单的字符串查询,没有编译时类型检查或Intellisen ...

  9. Intellij IDEA 编译等级与源代码等级不一致问题

    错误:Error:java: javacTask: source release 1.7 requires target release 1.7 原因:生成class字节码的java版本,低于了源代码 ...

  10. ActiveMQ配置高可用性的方式

    当一个应用被部署于生产环境,灾备计划是非常重要的,以便从网络故障,硬件故障,软件故障或者电源故障中恢复.通过合理的配置ActiveMQ,可以解决上诉问题.最典型的配置方法是运行多个Broker,一旦某 ...