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 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 == '\0')
{
if (cnt == 1 && pos->count)
return true;
else
return false;
}
for (int i = 0; i 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
Codeforces 514 D R2D2 and Droid Army(Trie树)的更多相关文章
- Codeforces 514 D R2D2 and Droid Army(RMQ+二分法)
An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, w ...
- Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]
传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【codeforces 514D】R2D2 and Droid Army
[题目链接]:http://codeforces.com/contest/514/problem/D [题意] 给你每个机器人的m种属性p1..pm 然后r2d2每次可以选择m种属性中的一种,进行一次 ...
- R2D2 and Droid Army(多棵线段树)
R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 861D - Polycarp's phone book 【Trie树】
<题目链接> 题目大意: 输入7e4个长度为9的字符串,每个字符串中只出现0~9这几种数字,现在需要你输出每个母串中最短的特有子串. 解题分析: 利用Trie树进行公共子串的判定,因为Tr ...
- 【Cf #291 B】R2D2 and Droid Army(二分,线段树)
因为题目中要求使连续死亡的机器人最多,令人联想到二分答案. 考虑如何检验这之中是否存在一段连续的长度为md的区间,其中花最多k步使得它们都死亡. 这个条件等价于区间中m个最大值的和不超过k. 枚举起点 ...
- Codeforces 633C Spy Syndrome 2(DP + Trie树)
题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...
- trie树 Codeforces Round #367 D Vasiliy's Multiset
// trie树 Codeforces Round #367 D Vasiliy's Multiset // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值 / ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
随机推荐
- 3.Shell的基本功能
3.Shell的基本功能Bash是Bourne-Again Shell的缩写.Bourne Shell的内部命令在Bash中同样适用.3.1 Shell语法3.1.1 Shell操作shell读取和执 ...
- axios配置
import axios, { isCancel } from 'axios' import { md5 } from 'vux' import util from '@/libs/util' imp ...
- 你不知道的css各类布局(二)之流体布局、液体布局、栅格布局
流体布局 什么是流 在谈论流体布局之前我们需要知道一件事情就是何为“流”,所谓“流”就是“文档流”,是css中的一种基本定位和布局 概念 流体布局(Liquid/Fluid Layout)指的是利用元 ...
- JavaScript例子1-给网页中所有<p>元素添加onclick事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- O063、NFS Volume Provider(Part II)
参考https://www.cnblogs.com/CloudMan6/p/5693771.html 本节开始创建 NFS volume ,操作方法和 LVM volume一样,唯一的区别是在 v ...
- 不同格式的YUV 和 RGB互转
YUV色彩空间: Y是亮度值,也就是说8位的灰度值即可组成一幅黑白图像,黑白电视机就是这样的. UV是色彩值,是给Y上色用的.U是Cb也就是RGB中的蓝色分量,V是Cr也就 ...
- 1 sql server中添加链接服务器
1 链接到另一个sql server 的实例 exec sp_addlinkedserver @server= '服务器的地址',@srvproduct='SQL Server' go 分布式查询中 ...
- app欢迎页问题
今天替换app中的图片,打包成apk后,欢迎页的图片怎么替换都还是旧的,尝试多次以后,确定以及肯定是替换成功了的,而且替换的也都对,只好清理了一下项目,重新build,最后再打包,结果成功了!真是坑! ...
- 巧用Map缓存提升"翻译"速度
在业务编码中,很多情况都需要用到code2Name或者id2Name之间的"翻译",在我的过往经历中发现不少开发人员都是直接双重循环实现这种"翻译".如果一次& ...
- Python 之 random模块
Python中的random模块用于生成随机数.1.random.random() #用于生成一个0到1的随机浮点数:0<= n < 1.0>>> random.ran ...