传送门

C. Watto and Mechanism
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position".

Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.

Input

The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively.

Next follow n non-empty strings that are uploaded to the memory of the mechanism.

Next follow m non-empty strings that are the queries to the mechanism.

The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.

Output

For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).

Sample test(s)
Input
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
Output
YES
NO
NO

题意:n个串,m次查询,每次给一个字符串,问在原串中能不能找到一个串,与之长度相同,且只有一个字符不同。

题解:没什么好说的,字典树,强行有个数组不开成全局变量,T哭了,,,,

9856760 2015-02-15 11:39:10 njczy2010 C - Watto and Mechanism GNU C++ Accepted 577 ms 99188 KB
9856712 2015-02-15 11:34:22 njczy2010 C - Watto and Mechanism GNU C++ Time limit exceeded on test 32 3000 ms 101200 KB
9856707 2015-02-15 11:33:54 njczy2010 C - Watto and Mechanism GNU C++ Memory limit exceeded on test 1 46 ms 262100 KB
9856697 2015-02-15 11:32:29 njczy2010 C - Watto and Mechanism GNU C++ Time limit exceeded on test 32 3000 ms 90700 KB
9856633 2015-02-15 11:26:42 njczy2010 C - Watto and Mechanism GNU C++ Time limit exceeded on test 32 3000 ms 89500 KB
9856553 2015-02-15 11:17:52 njczy2010 C - Watto and Mechanism GNU C++ Wrong answer on test 4 15 ms 70700 KB
9856517 2015-02-15 11:14:26 njczy2010 C - Watto and Mechanism GNU C++ Wrong answer on test 13 46 ms 70800 KB
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 300005
#define M 1505
//#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int fff[*N]; typedef struct
{
char v;
int mp[];
}PP; int tot;
int cnt[*N]; PP p[*N];
char s[*N]; void insert(int l)
{
int i;
int now=;
for(i=;i<l;i++){
if(p[now].mp[ s[i]-'a' ]==){
tot++;
p[now].mp[ s[i]-'a' ]=tot;
p[tot].v=s[i];
memset(p[tot].mp,,sizeof(p[tot].mp));
now=tot;
}
else{
now=p[now].mp[ s[i]-'a' ];
}
}
cnt[now]++;
} void ini()
{
memset(fff,,sizeof(fff));
int i;
int l;
tot=;
p[].v='z';
memset(p[].mp,,sizeof(p[].mp)); for(i=;i<=n;i++){
scanf("%s",s);
l=strlen(s);
fff[l]=;
insert(l);
}
//for(i=0;i<=tot;i++){
// printf(" i=%d v=%c cnt=%d\n",i,p[i].v,cnt[i]);
// }
} int check(int l,int cou,int now,int f)
{
// printf(" l=%d cou=%d now=%d v=%c cnt=%d f=%d\n",l,cou,now,p[now].v,cnt[now],f);
if(cou==l){
if(cnt[now]==) return ;
if(f==) return ;
else return ;
}
if(f>=) return ;
int i;
int ff;
for(i=;i<=;i++){
if(p[now].mp[i]!=){
if(s[cou]==i+'a'){
ff=check(l,cou+,p[now].mp[i],f);
}
else{
ff=check(l,cou+,p[now].mp[i],f+);
}
if(ff==) return ;
}
}
return ;
} void solve()
{
int i;
int l;
int flag;
for(i=;i<=m;i++){
scanf("%s",s);
l=strlen(s);
// printf("l=%d\n",l);
if(fff[l]==){
flag=;
}
else{
flag=check(l,,,);
}
if(flag==){
printf("YES\n");
}
else{
printf("NO\n");
}
}
} void out()
{ } int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d%d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}

Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]的更多相关文章

  1. hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...

  2. 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...

  3. Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串

    [题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...

  4. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  5. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  6. Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  7. Watto and Mechanism Codeforces Round #291 (Div. 2)

    C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  8. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  9. CF Watto and Mechanism (字典树+深搜)

    Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. CSS的相对定位和绝对定位

     relative的意思就是相对自己的一开始的位置进行的定位.如图: 但是这个元素的本身边距不变,还在原来位置   absolute的意思就是 如果它的父元素设置了除static之外的定位,比如pos ...

  2. URAL 1776 Anniversary Firework (概率,区间DP)

    坑,一开始以为,分成两半的时候去最大那个就行了, 实际上这样是不对的,因为有可能出现小的一半的时间比大的要长, 因为还和等待次数有关,且转移的时候需要用到次数更小的状态, 所以状态定义为二维,dp[i ...

  3. Ace 在HTML中使用方法

    <!DOCTYPE html> <html> <head> <title>Demo of ACE Editor</title> <!- ...

  4. Python 解压序列、可迭代对象并赋值给多个变量

    Python数据结构和类型 1.1 解压序列赋值给多个变量 现在有一个包含N个元素的元组或者是序列,怎样将它里面的值解压后同时赋值给N个变量? 解决思路:先通过简单的解压赋值给多个变量,前提是变量的数 ...

  5. gdb插件使用方法

    0x00 peda peda 安装: git clone https://github.com/longld/peda.git ~/peda echo "source ~/peda/peda ...

  6. baidumap demo(三)

    定位 您可以通过以下代码来开启定位功能: 源码复制打印关于 //开启定位功能 [_mapView setShowsUserLocation:YES]; 定位成功后,可以通过mapView.userLo ...

  7. EditorConfig文件

    EditorConfig .editorconfig文件 在很多开源项目中,会出现这个文件,这个文件有何作用? editorconfig 帮助开发者的(编辑器和IDEs)定义和维护编程风格. 有些编辑 ...

  8. codevs 数字三角形集结

    添在前面的一句话:初学DP,若有错误,请指出,不能误人子弟,欢迎大家提出意见.水平不高,博客写的比较粗糙,代码也挺丑,请见谅. 最原始的数字三角形: 1220 数字三角形  时间限制: 1 s  空间 ...

  9. Linux菜鸟起飞之路【三】Linux常用命令

    一.Linux命令的基本格式 命令 [选项] [参数] a)命令:就是告诉操作系统要做什么 b)选项:说明命令的运行方式,有的会改变命令的功能,选项通常以“-”开始 c)参数:说明命令的操作对象,如文 ...

  10. 王小胖之 Base64编码/解码

    使用场景:编码网址作为URL参数,简单编码或加密数据,下载地址生成或解析. 实现功能:BASE64在线编码和解码. 数据实例:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^& ...