zoukankan      html  css  js  c++  java
  • 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.

     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 = 100000;
    12 
    13 char wenhao[10] = {'a', 'b', 'c', 'd', 'e'};
    14 int position[5], 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] = '';
    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, 1));
    28         }
    29         return;
    30     }
    31     if (str[step] == '?') {
    32         for (int i = 0; i < 5; i++) {
    33             parten[tot++] = wenhao[i];
    34             dfs(step + 1);
    35             tot--;
    36         }
    37         dfs(step + 1);
    38     } else {
    39         parten[tot++] = str[step];
    40         dfs(step + 1);
    41         tot--;
    42     }
    43 }
    44 int main() {
    45     //freopen("inputG.txt", "r", stdin);
    46     ios::sync_with_stdio(false);
    47     cin.tie(0);
    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 = 0;
    57             ans = 0;
    58             cin >> str;
    59             len = str.length();
    60             dfs(0);
    61             cout << ans << endl;
    62         }
    63         mp.clear();
    64         book.clear();
    65     }
    66 
    67     return 0;
    68 }
  • 相关阅读:
    Yii2 使用 Beanstalk 队列系统
    Yii2 注册表单验证规则 手机注册时候使用短信验证码
    Yii2 高级模板 多域名管理问题
    PHP生成缩略图,控制图片质量,支持.png .jpg .gif
    yii2-lock-form 也许这就是你想要的,阻止表单多次提交
    PHP日期与时间戳转换
    PHP/Yii2操作Cookie,常见问题以及注意事项
    对称加密,API加密
    yii2弹出层
    两种不同的Context
  • 原文地址:https://www.cnblogs.com/Penn000/p/7472107.html
Copyright © 2011-2022 走看看