Codefroces 852 G. Bathroom terminal

G. 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 1abcaecaca?c

Output

3

Note

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

匹配查询

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int n,m,ans,a[5],len,k;
char s[110];
string mp;
set<string>v;
struct trie
{
    int cnt;
    struct trie *next[26];
    trie()
    {
        cnt=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
};
trie *root;
void insert(char *s)
{
    trie *p=root,*tmp;
    for(int i=0;s[i]!=‘\0‘;i++)
    {
        if(p->next[s[i]-‘a‘]==NULL)
        {
            tmp=new trie();
            p->next[s[i]-‘a‘]=tmp;
        }
        p=p->next[s[i]-‘a‘];
    }
    p->cnt++;
}
int find(string s)
{
    int x=s.size();
    trie *p=root;
    for(int i=0;i<x;i++)
    {
        if(p->next[s[i]-‘a‘]==NULL) return 0;
        p=p->next[s[i]-‘a‘];
    }
    return p->cnt;
}
void solve(int x)
{
    if(x==k)
    {
        mp="";
        for(int i=0;i<len;i++)
        {
            if(s[i]==‘`‘) continue;
            mp+=s[i];
        }
        if(!v.count(mp))
        {
            ans+=find(mp);
            v.insert(mp);
        }
        return ;
    }
    for(int i=-1;i<5;i++)
    {
        s[a[x]]=(char)(‘a‘+i);
        solve(x+1);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    root=new trie();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        insert(s);
    }
    while(m--)
    {
        ans=0;k=0;
        scanf("%s",s);
        len=strlen(s);
        v.clear();
        for(int i=0;i<len;i++)
            if(s[i]==‘?‘) a[k++]=i;
        solve(0);
        printf("%d\n",ans);
    }
    return 0;
}

map也可以,效率有点低

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<string>s;
map<string,int>mp;
string v;
char ch[75];
int a[5],n,m,ans,k,len;
void solve(int x)
{
    if(x==k)
    {
        v="";
        for(int i=0;i<len;i++)
        {
            if(ch[i]==‘`‘) continue;
            v+=ch[i];
        }
        if(!s.count(v))
        {
            ans+=mp[v];
            s.insert(v);
        }
        return ;
    }
    for(int i=-1;i<5;i++)
    {
        ch[a[x]]=(char)(‘a‘+i);
        solve(x+1);
    }
}
int main()
{
    ios::sync_with_stdio(true);
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>v;
        mp[v]++;
    }
    while(m--)
    {
        ans=0;k=0;
        scanf("%s",ch);
        len=strlen(ch);
        s.clear();
        for(int i=0;i<len;i++)
        {
            if(ch[i]==‘?‘) a[k++]=i;
        }
        solve(0);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-14 06:01:21

Codefroces 852 G. Bathroom terminal的相关文章

linux下虚拟终端terminator安装和使用

介绍terminator虚拟终端的安装和使用 安装 在Redhat6.8之下以提供有rpm包,直接使用yum就可以安装了 #yum install -y terminator 使用 1.可以在命令行调用 $terminator & 2.可以在桌面菜单中调用 Applications --> accessories --> terminator 简单好用的使用方法介绍: 分屏 1.1 垂直分割 快捷键 Ctrl+Shift+E 1.2 水平分隔 快捷键 Ctrl+Shift+O 1.3组

Ubuntu terminator 多窗口终端的快捷键

KEYBINDINGS The following keybindings can be used to control Terminator: Ctrl+Shift+O Split terminals Horizontally.(上下开新窗口) Ctrl+Shift+E Split terminals Vertically.(垂直开新窗口) Ctrl+Shift+Right Move parent dragbar Right.(放大当前窗口 向右) Ctrl+Shift+Left Move p

ubuntu14.04终端分屏terminator的安装使用与配置

ctrl+alt+t 打开终端 运行命令 sudo apt-get install terminator 安装终端程序terminator,安装完毕后按ctrl+alt+t打开终端如下图所示  这个终端程序可以分屏,常用操作快捷键如下: Ctrl+Shift+O Split terminals Horizontally.(上下开新窗口) Ctrl+Shift+E Split terminals Vertically.(垂直开新窗口) Ctrl+Shift+Right Move parent dr

Linux下安装gcc 、g++ 、gfortran编译器

一.ubuntu下gcc/g++/gfortran的安装 1.安装 (1).gcc ubuntu下自带gcc编译器.可以通过“gcc -v”命令来查看是否安装. (2).g++ 安装g++编译器,可以通过命令“sudo apt-get install build-essential”实现. 执行完后,就完成了gcc,g++,make的安装.build-essential是一整套工具,gcc,libc等等. 通过“g++ -v”可以查看g++是否安装成功. 注:“sudo apt-get inst

Linux/Mac OS 个人常用Terminal技巧整理

刚开始接触linux有些不适应,走了不少弯路,一直没有系统的学过linux应用,基本都是零零散散Google出来的知识,在这里做个整理: Vi/Vim 基本操作: 刚开始接触linux时,不懂vi吃了不少亏.在命令行状态下,一旦用vi打开了文本只能通过重启来退出. vi/vim 分三种模式: 指令模式,编辑模式,选择模式. 只有在编辑模式下才能进行输入(不是绝对). 默认是"指令模式", 这个模式只支持vi/vim的指令, 在敲键盘时只会滴滴响. 不知道有多少人被这种方式搞晕过(晕不晕

Research of Hydro-Turbine Governor Supplementary Control Strategy for Islanding AC Grid at Sending Terminal of HVDC System

G. Zhang, Y. Cheng, N. Lu and Q. Guo, "Research of Hydro-Turbine Governor Supplementary Control Strategy for Islanding AC Grid at Sending Terminal of HVDC System," in IEEE Transactions on Energy Conversion, vol. 31, no. 4, pp. 1229-1238, Dec. 20

terminal使用小技巧

直接上配置,你懂的 lai'mac:~ laijingli$ more .bash_profile ###add my scripts to the search path for convenient daily usage export PATH=$PATH:/Users/laijingli/autoshell:/opt/local/bin:/opt/local/sbin ###alias for normal use alias grep='grep --color' ###change

Mac下OpenCV开发环境配置(Terminal和Xcode)

亲证可用:http://www.jianshu.com/p/11959977589a Mac OS X 10.1 Xcode 7.2(7C68) OpenCV 2.4.13 Mac OS10.11 OpenCV for Linux/Mac 下载OpenCV后,cd进入指定目录,使用unzip命令解压请提前安装Homebrew命令先安装cmake工具然后cd进入解压后的文件夹 $ brew install cmake $ mkdir release $ cd release $ cmake -G

解决g++:command not found参考

在安装一些包的时候,遇到g++:command not found error,这是由于系统缺失相对应的包 工具/原料 CentOS64 方法/步骤 在发现该错误时,先在系统中确认是否缺失相对应的包: rpm -qa | grep "g++" 在确认系统未安装之后,查询可安装的相对应的功能的包: yum whatprovides "*/g++" 根据提供的包,选择适合系统的进行安装: yum install gcc-c++-4.4.7-4.el6.x86_64 4