传送门

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. ZendStudio 常用快捷键大全

    应用场景 快捷键 功能 查看快捷键 ctrl+shift+l 显示所有快捷键列表 查看和修改快捷键   打开Window->Preferences->General->keys 修改 ...

  2. GloVe:另一种Word Embedding方法

    若想深层地理解GloVe和本文,最好了解SVD, word2vec(skip-gram为主)的相关知识.若仅寻求一种新的word embedding方法,可以不必了解以上前置知识. 一言以蔽之,Glo ...

  3. common-configuration的一些应用

    此程序依赖commons-configuration-1.6.jar和commons-lang-2.6.jar两个jar包. 需要先在工程的src目录下建立如下几个文件: config.propert ...

  4. 对于WebAssembly编译出来的.wasm文件js如何调用

    WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍 主要是让大家知道在js里面如何调用执行它,我之前看WebAssemblyAPI时候反正是看得一脸懵逼 也是为了大家能更快的 ...

  5. 【Java_基础】JVM内存模型与垃圾回收机制

    1. JVM内存模型 Java虚拟机在程序执行过程会把jvm的内存分为若干个不同的数据区域来管理,这些区域有自己的用途,以及创建和销毁时间. JVM内存模型如下图所示 1.1 程序计数器 程序计数器( ...

  6. 常用JS方法整理

    目录: 截取指定字节数的字符串 判断是否微信 获取时间格式的几个举例 获取字符串字节长度 对象克隆.深拷贝 组织结构代码证验证 身份证号验证 js正则为url添加http标识 URL有效性校验方法 自 ...

  7. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  8. linux下C++的多线程编程

    1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...

  9. Repo command reference

    Repo command reference In this document init sync upload diff download forall prune start status Rep ...

  10. 【MySQL】Got fatal error 1236原因和解决方法

    一 前言  MySQL 的主从复制作为一项高可用特性,用于将主库的数据同步到从库,在维护主从复制数据库集群的时候,作为专职的MySQL DBA,笔者相信大多数人都会遇到“Got fatal error ...