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 ...
随机推荐
- 平凡主丛上的Yang-Mills理论
本文是复旦大学由丁青教授的暑期课程“Yang-Mills理论的几何及其应用”所作笔记,会有少许修正. 所需基础: 多元微积分学 微分方程(常微分方程,数学物理方程) 曲线曲面论(初等微分几何) 以下是 ...
- 【BZOJ 1212】[HNOI2004]L语言
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 因为查询的字典里面.单词的最大长度为10 所以. 如果建立一棵字典树的话. 深度最多为10: 那么可以写一个DP; 设f[i]表示1 ...
- Docker学习总结(10)——10分钟玩转Docker
1.前言 进入云计算的时代,各大云提供商AWS,阿里云纷纷推出针对Docker的服务,现在Docker是十分火爆,那么Docker到底是什麽,让我们来体验一下. 2.Docker是什麽 Docker是 ...
- mysql中文乱码解决方式
近期项目使用到mysql.却突然出现了中文乱码问题.尝试了多种方案,最终解决乱码问题,总结一下解决方式,给遇到同样问题的人一点參考. 中文乱码的原因 1.安装mysqlserver的时候编码集设定有问 ...
- hadoop-15-Ambari进行HDP、zookeeper安装
hadoop-15-Ambari进行HDP.zookeeper安装 1,登陆server_1:8080 admin/admin 2,命名:Hdp_cluster 3,输入HDP.HDP_Utils 地 ...
- GetKeyState(), GetAsyncKeystate(), GetKeyboardSlate()
GetKeyState.GetAsyncKeyState.GetKeyboardState函数的差别: 1.BOOL GetKeyboardState( PBYTE lpKeyState );获得全部 ...
- 具体解释C++引用——带你走进引用的世界
一.介绍引用 首先说引用是什么,大家能够记住,引用就是一个别名,比方小王有个绰号叫小狗.他的妈妈喊小狗回家吃饭.那就是在喊小王回家吃饭. 接下来我们用两行代码来声明一个引用(就拿小王和小狗来说吧 ...
- HTML5_Web_SQL_Database数据库
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiEAAAEGCAIAAAA8NNLDAAAgAElEQVR4nOxdd5hcVdk/SUgghCSEhC ...
- mysql创建新用户时 连接报错 (解决方案把匿名用户删除)
ERROR (): Access denied for user 'ljcc'@'localhost' (using password: YES) 步骤 创建了mysql的用户 insert mysq ...
- jquery实现上下浮动
jquery实现上下浮动: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...