传送门

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. uva12264 Risk

    最小值最大,就二分判断. map[i] = '0'+map[i];这样更方便 每个点拆成i,i’,  S连i,cap为a[i],i’连T,cap为1(保证至少剩一个)或mid. i,i’ ,a[i] ...

  2. Injection of autowired dependencies failed;错误解决

    代码自动生成的时候可能出现这个问题,反正我是找了半天才发现.serviceimp层不要写抽象类的声明abstract,这个删掉.

  3. Android之父Andy Rubin:被乔布斯羡慕嫉妒的天才

    今年中国掀起一股“苹果热”,智能手机iPhone.平板电脑iPad遭疯抢,一度卖断货.然而,令许多人意想不到的是,在“苹果”的老家——美国市场,智能手机中卖得最火的并不是iPhone,而是Androi ...

  4. Java习题附答案

    第一章练习题(Java入门) 1.下列哪项不是JDK所包含的内容?(选一项)C 红色代表正确答案 A.Java编程语言 B.工具及工具的API C.Java EE扩展API D.Java平台虚拟机 2 ...

  5. No-11.变量进阶

    变量进阶 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递 ...

  6. 编写shellcode的几种姿势

    今天开始在做hitcon-training的题目,做到lab2就发现了自己的知识盲区,遇到无法执行shell的情况,需要自己打shellcode执行cat flag 操作 经过一系列的搜索,发现了几种 ...

  7. JDBC连接数据库详解

    JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),这通过java.la ...

  8. 洛谷P1001 A+B Problem

    这道题…………还是很简单!!! code: #include <iostream> #include <cstdio> using namespace std; int mai ...

  9. golang 实现冒泡排序

    package main import ( "fmt" ) func main(){ a := [...] int{2,5,9,6,8} fmt.Println(a) num := ...

  10. 关于ajax在微信智能客服管理端的使用

    ajax的语法样例: $.ajax({ 'url':url, 'type':'GET', 'dataType':'json', 'data':data, success:function (data) ...