Codefroces 852 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.
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 contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.
3 1
abc
aec
ac
a?c
3
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的更多相关文章
- 【BubbleCup X】G:Bathroom terminal
一个hash的题 对?出现位置直接暴力枚举,然后hash判断下,扔进map里 cf的评测机跑的针tm块 #include<bits/stdc++.h> ; ; typedef long l ...
- Codeforces 852G Bathroom terminal 【Trie树】
<题目链接> 题目大意: 现在给定出n个字符串,并且进行m此询问,每次询问给出一个匹配串,每次询问都给出该匹配串能够匹配的字符串个数(题目只出现字符'a'~'e').'?'可以看成任意字符 ...
- Codeforces852G(字符串hash)
G. Bathroom terminal time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...
- ubuntu14.04终端分屏terminator的安装使用与配置
安装 terminator 软件 . sudo apt-get install terminator 这个终端程序可以分屏,常用操作快捷键如下: Ctrl+Shift+O Split terminal ...
- 一个关于react-native的demo,详细请转GitHub
react native 0 介绍 支持ios和android两个平台 下载:git clone https://github.com/chunlei36/react-native-full-exam ...
- Linux Terminator
NAME Terminator - Multiple GNOME terminals in one window SYNOPSIS terminator [options] DESCRIPTION T ...
- IOS越狱开发错误解决
Questions: haseScriptExecution Run\ Script /Users/jun/Library/Developer/Xcode/DerivedData/ButtonMa ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
随机推荐
- FarPoint自动换行
单元格自动换行 FarPoint.Win.Spread.CellType.TextCellType mType = new FarPoint.Win.Spread.CellType.TextCellT ...
- Hadoop的datanode超时时间设置
datanode进程死亡或者网络故障造成datanode无法与namenode通信, namenode不会立即把该节点判定为死亡,要经过一段时间,这段时间暂称作超时时长. HDFS默认的超时时长为10 ...
- C语言中头文件尖括号和引号的区别
用include 引用头文件时,双引号和尖括号的区别: 1.双引号:引用非标准库的头文件,编译器首先在程序源文件所在目录查找,如果未找到,则去系统默认目录查找,通常用于引用用户自定义的头文件. 2.尖 ...
- Qt之QSpacerItem
简述 QSpacerItem类为布局提供了一个空白区. 简述 比对 使用 效果 源码 效果 源码 比对 通常情况下,不需要直接使用这个类,Qt内建布局管理器在操控空白区时提供以下功能: 类 函数 QH ...
- 智课雅思词汇---十、pend是什么意思
智课雅思词汇---十.pend是什么意思 一.总结 一句话总结:[词根含义]:悬挂,垂;称量;支付 词根:-pend-, -pens- [词根含义]:悬挂,垂;称量;支付 [词根来源]:来源于拉丁语动 ...
- spark scala word2vec 和多层分类感知器在情感分析中的实际应用
转自:http://www.cnblogs.com/canyangfeixue/p/7227998.html 对于威胁检测算法使用神经网络训练有用!!!TODO待实验 /** * Created by ...
- zzulioj--1805-- SC和ta的游泳池(简单几何)
1805: SC和ta的游泳池 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 154 Solved: 43 SubmitStatusWeb Boar ...
- NOIP2017 小凯的疑惑 解题报告(赛瓦维斯特定理)
题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...
- spring的quartz定时任务
一.版本: 1.spring:4.1.7: 2.quartz:2.2.1: 二.基于ssm项目: 1.引入jar包:quartz-2.2.1.jar:spring所需包. 2.说明:quartz ...
- 使用tinyxml2库解析xml
tinyxml2简介 tinyxml2是c++编写的轻量级的xml解析器,而且是开放源代码的,在一些开源的游戏引擎中用的比较多.源码托管在github上. 源码地址:https://github.co ...