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. const int *a与int *const a,const int *const a的区别

    来源:https://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰 ...

  2. PatentTips - Method for network interface sharing among multiple virtual machines

    BACKGROUND Many computing systems include a network interface card (NIC) to provide for communicatio ...

  3. find the safest road HDU杭电1596【Dijkstra || SPFA】

    pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...

  4. ORA-06553:PLS-306:wrong number or types of arguments in call to &#39;&#39;

    1.错误描写叙述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...

  5. EJB3.0高速入门项目开发步骤

    EJB3.0开发步骤 1.   开发环境 IDE开发工具:Eclipse Java EE IDE for Web Developers EJB容器:jboss-4.2.3.GA 后台数据库:MysQL ...

  6. IPS

    转自:http://www.ithome.com.tw/node/79293 企業中網路環境的防護,通常都是先有防火牆,再搭配IPS.但是,實際上買得起IPS防護的企業有限,這是因為IPS的價格很昂貴 ...

  7. ListView中嵌套GridView点击事件

    做一个项目时,需要在ListView中嵌套GridView,因为ListView的每个条目中不一定出现GridView,那么问题来了,添加GridView的Item的点击事件后,有GridView出现 ...

  8. Android 代码中使用Color工具类 parseColor

    方式一: arg1.setBackgroundColor(Color.parseColor("#87CEFA")); 方式二: arg1.setBackgroundColor(ge ...

  9. hdu 4825 xor sum(字典树+位运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  10. 因子问题 I - Ugly Numbers

    题目: Ugly numbers are numbers whose only prime factors are 2, 3 or 5 . The sequence 1, 2, 3, 4, 5, 6, ...