Bubble Cup X - Finals [Online Mirror] G. Bathroom terminal(STL)

Description

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

题目大意

先给定m个字符串,再给一个含有’?’(至多包含3个)的字符串,’?’可以替换成字符’a’~’e’,或者直接去掉,然后在m个字符串中查找有多少个可以实现完全匹配。

解题思路

暴力

代码实现

#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
using namespace std;
map<string,int>mp;
set<string>ss;
string str;
void dfs(string st)
{
    int len=st.size(),i=0;
    while(st[i]!='?'&&i<len) i++;
    if(i==len)
    {
        ss.insert(st);
        return ;
    }
    for(char ch='a'; ch<='e'; ch++)
    {
        st[i]=ch;
        dfs(st);
    }
    dfs(st.substr(0,i)+st.substr(i+1,len-1));
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n,m;
    cin>>m>>n;
    mp.clear();
    for(int i=0; i<m; i++)
    {
        cin>>str;
        mp[str]++;
    }
    for(int i=0; i<n; i++)
    {
        int ans=0;
        ss.clear();
        cin>>str;
        dfs(str);
        set<string>::iterator it;
        for(it=ss.begin(); it!=ss.end(); it++)
        {
            if(mp[*it]>0)
                ans+=mp[*it];
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值