传送门

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. UVA 12563 Jin Ge jin Qu [h] ao 劲歌金曲 (01背包)

    每首只能唱一次,而且中间不能不唱歌,所以先把状态赋值为-1,以区别合法状态和非法状态,在唱歌曲目最多的条件下,离开时间应该尽量晚. 状态定义f[i][j]考虑前i首歌唱歌时间为j的最大唱歌曲目 #in ...

  2. stringstream类的简介和用法

    一.简介 <sstream>类库定义了三种类:istringstream,ostringstream,stringstream.分别用来进行流的输入,流的输出,输入输出操作.在此演示str ...

  3. Kubernetes介绍与特性

    1.Kubernetes 是什么 简单的来说,k8s可以理解为,一个容器平台,一个微服务平台,便携式云平台,我们那可以很快速的搭建一个服务,快速的运行起来 2.Kubernetes特性

  4. 2010: C语言实验——逆置正整数

    2010: C语言实验——逆置正整数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 949  Solved: 691[Submit][Status][We ...

  5. OpenCV2:第十一章 图像转换

    一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...

  6. Kernel Stack Overflow(转)

    0x00 漏洞代码 stack_smashing.c #include <linux/init.h> #include <linux/module.h> #include &l ...

  7. shell脚本,awk数组之如何处理多个文件。

    [root@localhost | > file [root@localhost - | > file1 [root@localhost awk]# cat file [root@loca ...

  8. SimpleWeather APP

    参考 iOS 7 Best Practices; A Weather App Case Study: Part 1/2 iOS 7 Best Practices; A Weather App Case ...

  9. Watch Before You Feel Pressure

    Today's assembly is about the start of a journey. 今天的大会是一个旅程的开始. The start of the rest of your lives ...

  10. [LUOGU] 2820 局域网

    题目背景 某个局域网内有n(n<=100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内的连接形成了回路,我们知道如果局域网形成回路那么数据将不停的在回路内传输,造成网络卡的现象.因为连 ...