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. HTTP——学习笔记(7)

    HTTP中的认证机制 什么是认证机制?: 服务器需要知道客户端是谁. 怎样知道客户端身份?: 核对“登录者本人才知道的信息” 密码:只有本人才会知道的字符串信息 动态令牌:仅限本人持有的设备内显示的一 ...

  2. 使用JMX透过防火墙远程监控tomcat服务

    https://my.oschina.net/mye/blog/64879 http://blog.csdn.net/l1028386804/article/details/51547408 http ...

  3. Linux 进程间通信(IPC)

    Linux 进程间通信(IPC): Linux系统中除了进程和进程之间通信,我想大家也应该关注用户空间与内核空间是怎样通信的.例如说netlink等等. 除了传统进程间通信外像Socket通信也须要掌 ...

  4. glm编译错误问题解决 formal parameter with __declspec(align(&#39;16&#39;)) won&#39;t be aligned

    參考:http://stackoverflow.com/questions/25300116/directxxmmatrix-error-c2719-declspecalign16-wont-be-a ...

  5. tomcatserver管理界面username和password忘记

    tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...

  6. 安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above. (错误解决方法)

    安卓安装提示:Android SDK requires Android Developer Toolkit version 21.1.0 or above.  (错误解决方法) 主要是因为版本号不正确 ...

  7. 小米净水器与小区过滤价格水对照.xls

    总结:要是一天用水量为7升下面.还是用小区的过滤水为好,合算. 假设过滤水需求量大,可能小米的净水器比較好.当然,小区的要天天去接.要求风雨无阻的. 这点小米的随用随接就更好. 注意一点,小米的还要用 ...

  8. 范型在java中的应用

    根据泛型在java中的不同位置,大致可以分为类泛型.方法泛型和接口泛型.以下三个Demo基本展现三种泛型的用法,其中接口泛型又分两种情况描述.类泛型和方法泛型 import java.util.Arr ...

  9. 关于node的聊天室错误

    Deprecationwarning:process,EventEmitter is deprecated use require ('events')instead 关于node的聊天室错误 > ...

  10. c++几种排序算法代码

    #include <iostream> #include <vector> using namespace std; //交换int void swap(int& a, ...