zoukankan
html css js c++ java
Codeforces 514 D R2D2 and Droid Army(Trie树)
[题目链接](http://codeforces.com/problemset/problem/514/D) 大意是判断所给字符串组中是否存在与查询串仅一字符之差的字符串。 关于字符串查询的题,可以用[字典树(Trie树)](http://www.cnblogs.com/orangee/p/8912971.html)来解,第一次接触,做个小记。在查询时按题目要求进行查询。 代码: ```C++ #define _CRT_SECURE_NO_DEPRECATE #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; typedef long long ll; typedef pair
P; typedef map
M; typedef vector
V; typedef queue
Q; const int maxn = 6 * 100000 + 10; const int N = 3; struct trie { trie* next[N]; int count; }; typedef trie* link; link create() { link p = new trie; p->count = 0; for (int i = 0; i < N; ++i) p->next[i] = NULL; return p; } void insert(char* s, link root) { char* p = s; link node = root; while (*p) { if (node->next[*p - 'a']==NULL) node->next[*p - 'a'] = create(); node = node->next[*p - 'a']; ++p; } node->count++; return; } bool query(char* s, link pos,int cnt) { if (*s == ' ') { if (cnt == 1 && pos->count) return true; else return false; } for (int i = 0; i < N; ++i) { if (i != *s - 'a' && cnt==0 && pos->next[i]) { if (query(s + 1, pos->next[i], 1)) return true; } if (i == *s - 'a' && pos->next[i]) { if (query(s + 1, pos->next[i], cnt)) return true; } } return false; } char s[maxn]; int main() { int n,m,k,i,j; link root=create(); cin >> n >> m; for (i = 0; i < n; ++i) { scanf("%s", s); insert(s, root); } for (i = 0; i < m; ++i) { scanf("%s", s); if (query(s, root, 0)) cout << "YES "; else cout << "NO "; } return 0; } ```
查看全文
相关阅读:
Window 中的自带方法Base64
React_Class1
npm 常用操作
React__生命周期
axios 简单方法个人理解
JavaScript_Window对象
常见的搜索引擎技巧
JS_String常见问题Demo
java调用C++代码
java虚拟机指令dup的理解
原文地址:https://www.cnblogs.com/orangee/p/8913065.html
最新文章
迭代器适配器(一)back_inserter和front_inserter的实现
标准库priority_queue的一种实现
标准库Queue的实现
标准库Stack的一种实现
借助backtrace和demangle实现异常类Exception
标准库Allocator(三)uninitialized_fill等函数的实现
标准库Allocator的简易实现(二)
标准库Allocator的使用(一)
简单的内存分配器
扩充STL-编写自己的迭代器
热门文章
Vector的一种实现(二)
改变框架大小(解决浏览器不识别)
ajax
完美运动框架
简单理解laravel中的Ioc控制反转(依赖注入)
insteadof
反射api
第一篇
React_PureComponent 简化 react shouldComponentUpdate 方法,优化性能
React_loadable
Copyright © 2011-2022 走看看