博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF Watto and Mechanism (字典树+深搜)
阅读量:5758 次
发布时间:2019-06-18

本文共 3304 字,大约阅读时间需要 11 分钟。

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 同样的思路,错了很多次,看来实现能力还是不够强。 字典树加搜索,没什么好说的,注意必须要改变一个字母就行。
1 #include 
2 using namespace std; 3 4 const int SIZE = 800000; 5 struct Node 6 { 7 Node * next[3]; 8 bool word; 9 }* ROOT = nullptr;10 char S[SIZE];11 12 bool dfs(char *,Node *,bool);13 int main(void)14 {15 int n,m;16 char ch;17 ROOT = new Node;18 for(int i = 0;i < 3;i ++)19 {20 ROOT -> next[i] = nullptr;21 ROOT -> word = false;22 }23 24 cin >> n >> m;25 cin.ignore();26 for(int i = 0;i < n;i ++)27 {28 Node * cur = ROOT;29 while(cin.get(ch) && ch >= 'a' && ch <= 'c')30 {31 if(cur -> next[ch - 'a'])32 cur = cur -> next[ch - 'a'];33 else34 {35 cur -> next[ch - 'a'] = new Node;36 cur = cur -> next[ch - 'a'];37 for(int i = 0;i < 3;i ++)38 cur -> next[i] = nullptr;39 cur -> word = false;40 }41 }42 cur -> word = true;43 }44 for(int i = 0;i < m;i ++)45 {46 cin >> S;47 if(dfs(S,ROOT,true))48 puts("YES");49 else50 puts("NO");51 }52 53 return 0;54 }55 56 bool dfs(char * s,Node * t,bool chance)57 {58 if(!t)59 return false;60 if(!(*s))61 {62 if(t -> word && !chance)63 return true;64 return false;65 }66 67 if(dfs(s + 1,t -> next[*s - 'a'],chance))68 return true;69 if(chance)70 for(int i = 0;i < 3;i ++)71 if(i != (*s - 'a'))72 if(dfs(s + 1,t -> next[i],false))73 return true;74 return false;75 }

 

转载于:https://www.cnblogs.com/xz816111/p/4450430.html

你可能感兴趣的文章
28. PowerShell -- 注册表操作
查看>>
搭建 android sdk环境
查看>>
第14章 grep、sed、awk 正则表达式
查看>>
ant_Jmeter持续集成测试报告优化之添加throughput显示
查看>>
一个=号引发的错误.......
查看>>
CPU显卡内存与3DMAX渲染的关系
查看>>
【Java】方法重载于覆写的区别;This与Super的区别;Final关键字的作用
查看>>
10.2生成器
查看>>
VS2010编辑界面主题美化
查看>>
Linux 常用命令之touch
查看>>
RestTemplate设置通用header
查看>>
TRex 学习(2) ---- stateful (basic)
查看>>
[高并发Java 二] 多线程基础
查看>>
PHP源码目录结构
查看>>
Linux桌面虚拟化技术KVM介绍及其安装
查看>>
硬盘主引导记录详解
查看>>
用户与用户组管理
查看>>
CentOS 6.8 手工安装 Firefox
查看>>
【栈】POJ 1028 Web Navigation
查看>>
[文摘]JDK里的设计模式
查看>>