Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
3 seconds
256 megabytes
standard input
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.
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'.
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).
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
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 [字典树]的更多相关文章
- hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...
- Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串
[题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...
- 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 ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- 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 ...
- 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 ...
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- CF Watto and Mechanism (字典树+深搜)
Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- spark 的RDD各种转换和动作
今天先把spark的各种基本转换和动作总结下,以后有时间把各种用法放上去. 1 RDD基本转换操作 map.flagMap.distinct coalesce.repartition coale ...
- 第009课 gcc和arm-linux-gcc和MakeFile
from:第009课 gcc和arm-linux-gcc和MakeFile 第001节_gcc编译器1_gcc常用选项_gcc编译过程详解 gcc的使用方法 gcc [选项] 文件名 gcc常用选项 ...
- jquery动态实现填充下拉框
当点下拉框时动态加载后台数据. 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) ...
- 获取 request 中 json 数据
import java.io.IOException; import javax.servlet.http.HttpServletRequest; /** * request 对象的相关操作 * @a ...
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
首先,找齐Spring框架中IoC功能.aop功能.JdbcTemplate功能所需的jar包,当前13个Jar包 1.Spring压缩包中的四个核心JAR包,实现IoC控制反转的根据xml配置文件或 ...
- WYS APP
UI图:http://modao.io/app/H8eZCQdV1pskjQ7z8bLh 四个tab:我要赛.赛事.运动吧.个人中心 赛事页面 1.主要是个NavigationController 2 ...
- 如何用纯 CSS 创作一盘传统蚊香
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BVpvMz 可交互视频教 ...
- node.js----服务器http
请求网址过程: 1.用户通过浏览器发送一个http的请求到指定的主机 2.服务器接收到该请求,对该请求进行分析和处理 3.服务器处理完成以后,返回对应的数据到用户机器 4.浏览器接收服务器返回的数据, ...
- Python9-面对对象2-day23
#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...
- Oracle联合主键
转https://www.cnblogs.com/king-xg/p/6721272.html alter table tablename add constraint unionkeyname pr ...