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 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.

匹配查询

#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[],len,k;
char s[];
string mp;
set<string>v;
struct trie
{
int cnt;
struct trie *next[];
trie()
{
cnt=;
for(int i=;i<;i++)
next[i]=NULL;
}
};
trie *root;
void insert(char *s)
{
trie *p=root,*tmp;
for(int i=;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=;i<x;i++)
{
if(p->next[s[i]-'a']==NULL) return ;
p=p->next[s[i]-'a'];
}
return p->cnt;
}
void solve(int x)
{
if(x==k)
{
mp="";
for(int i=;i<len;i++)
{
if(s[i]=='`') continue;
mp+=s[i];
}
if(!v.count(mp))
{
ans+=find(mp);
v.insert(mp);
}
return ;
}
for(int i=-;i<;i++)
{
s[a[x]]=(char)('a'+i);
solve(x+);
}
}
int main()
{
scanf("%d%d",&n,&m);
root=new trie();
for(int i=;i<n;i++)
{
scanf("%s",s);
insert(s);
}
while(m--)
{
ans=;k=;
scanf("%s",s);
len=strlen(s);
v.clear();
for(int i=;i<len;i++)
if(s[i]=='?') a[k++]=i;
solve();
printf("%d\n",ans);
}
return ;
}

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[];
int a[],n,m,ans,k,len;
void solve(int x)
{
if(x==k)
{
v="";
for(int i=;i<len;i++)
{
if(ch[i]=='`') continue;
v+=ch[i];
}
if(!s.count(v))
{
ans+=mp[v];
s.insert(v);
}
return ;
}
for(int i=-;i<;i++)
{
ch[a[x]]=(char)('a'+i);
solve(x+);
}
}
int main()
{
ios::sync_with_stdio(true);
cin>>n>>m;
for(int i=;i<n;i++)
{
cin>>v;
mp[v]++;
}
while(m--)
{
ans=;k=;
scanf("%s",ch);
len=strlen(ch);
s.clear();
for(int i=;i<len;i++)
{
if(ch[i]=='?') a[k++]=i;
}
solve();
printf("%d\n",ans);
}
return ;
}

Codefroces 852 G. Bathroom terminal的更多相关文章

  1. 【BubbleCup X】G:Bathroom terminal

    一个hash的题 对?出现位置直接暴力枚举,然后hash判断下,扔进map里 cf的评测机跑的针tm块 #include<bits/stdc++.h> ; ; typedef long l ...

  2. Codeforces 852G Bathroom terminal 【Trie树】

    <题目链接> 题目大意: 现在给定出n个字符串,并且进行m此询问,每次询问给出一个匹配串,每次询问都给出该匹配串能够匹配的字符串个数(题目只出现字符'a'~'e').'?'可以看成任意字符 ...

  3. Codeforces852G(字符串hash)

    G. Bathroom terminal time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

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

    安装 terminator 软件 . sudo apt-get install terminator 这个终端程序可以分屏,常用操作快捷键如下: Ctrl+Shift+O Split terminal ...

  5. 一个关于react-native的demo,详细请转GitHub

    react native 0 介绍 支持ios和android两个平台 下载:git clone https://github.com/chunlei36/react-native-full-exam ...

  6. Linux Terminator

    NAME Terminator - Multiple GNOME terminals in one window SYNOPSIS terminator [options] DESCRIPTION T ...

  7. IOS越狱开发错误解决

      Questions: haseScriptExecution Run\ Script /Users/jun/Library/Developer/Xcode/DerivedData/ButtonMa ...

  8. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  9. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

随机推荐

  1. Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)

    在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...

  2. [转载]不唐突的JavaScript的七条准则

    经过多年的开发.教学和编写不唐突的JavaScript, 我发现了下面的一些准则.我希望它们可以帮助你对“为什么这样设计和执行JavaScript比较好”有一点理解.这些规则曾经帮助我更快地交付产品, ...

  3. NYIST 1108 最低的惩罚

    最低的惩罚 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 那么现在问题就来了... 给你N(1=<N<=15)个任务,每个任务有一个截止完成时间t(1= ...

  4. 2016 10 27 考试 dp 向量 乱搞

    目录 20161027考试 T1: T2: T3: 20161027考试 考试时间 7:50 AM to 11:15 AM 题目 考试包 据说这是一套比较正常的考卷,,,嗯,,或许吧, 而且,,整个小 ...

  5. ubuntu 各种窗体操作

    通用 ctrl+alt+0~9 改变窗体大小和是否显示 alt+F4 关闭窗体菜单键+相应启动器位置的编号打开程序 ctrl+pageup/pagedown 在tab间移动 ctrle+shift+p ...

  6. android自己定义圆盘时钟

    自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的view里面,在view里面仅仅须要绘制秒针和分针时针并控其转动就可以. 下面就是自己定义view的主要代码: pa ...

  7. IIS访问站点,出现connection refused

    排查后,发现是因为使用了代理导致的. 需要设置 Don't use the proxy server for local addresses.

  8. HTML5,CSS3新特性,与旧版的区别

    HTML5新特性 (1)语意化更好的内容元素,比如 article.footer.header.nav.section (2)本地存储.sessionStorage.localStorage和inde ...

  9. iOS: 零误差或极小误差的定时执行或延迟执行?

    问题如下: 节奏类游戏需要执行很多的跟音乐节拍相关的操作,并且为了保证节奏感,需要让操作跟节拍的关系十分紧密.对两者间隔要求不能超过0.02秒或更低. 目前使用了 GCD 中的 asyncAfter( ...

  10. [ Git ] [ GitHub ] 如何刪除自己github上面的Repository-轉載

    http://www.winwu.cc/2013/03/githubrepository.html