hdu5687 字典树
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+, n = ;
struct node {
int next[];
int cnt;
void init() {
cnt = ;
memset(next,-,sizeof(next));
}
};
node trie[maxn*n];
int tot = ;
void insert(char *s) {
int cur = , len = strlen(s);
for (int i = ; i < len; i++) {
int temp = s[i]-'a';
int next = trie[cur].next[temp];
if (next == -) {
next = ++tot;
trie[next].init();
trie[cur].next[temp] = next;
}
cur = next;
trie[cur].cnt++;
}
}
bool find(char *s) {
int cur = , len = strlen(s);
for (int i = ; i < len; i++) {
int temp = s[i]-'a';
int next = trie[cur].next[temp];
if (next == -) return false;
cur = next;
}
return trie[cur].cnt > ;
}
void del(char * s) {
int cur = , late, len = strlen(s);
for (int i = ; i < len; i++) {
int temp = s[i]-'a';
int next = trie[cur].next[temp];
if (next == -) return;
late = cur;
cur = next;
}
cur = ;
for (int i = ; i < len; i++) {
int temp = s[i]-'a';
int next = trie[cur].next[temp];
late = cur;
cur = next;
trie[cur].cnt--;
}
trie[cur].init();
int temp = s[len-]-'a';
trie[late].next[temp] = -;
}
int main() {
int n; scanf("%d",&n);
char cmd[], word[];
for (int i = ; i <= n; i++) {
scanf("%s%s",cmd,word);
if (cmd[] == 'i') insert(word);
else if (cmd[] == 's') {
if (find(word)) puts("Yes");
else puts("No");
}
else del(word);
}
return ;
}
hdu5687 字典树的更多相关文章
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- 只会Vue怎么开发小程序?vue和微信小程序的到底有哪些区别?
写了vue项目和小程序,发现二者有许多相同之处,在此想总结一下二者的共同点和区别. 一.生命周期 先贴两张生命周期图对比下: vue生命周期 小程序生命周期 相比之下,小程序的钩子函数要简单得多. v ...
- thinkphp5 -- _initialize()初始化控制器
public function _initialize() { parent::_initialize(); } public function __construct() { $; parent:: ...
- opencv-4-成像系统与Mat图像颜色空间
opencv-4-成像系统与Mat图像颜色空间 opencvc++qtmat 目标 知道 opencv 处理图像数据的格式 介绍 mat 基础内容 知道 BGR 颜色 显示 颜色转换 BGR 到 灰度 ...
- Latex-0-latex2word
Latex-0-latex2word LatexXeLaTex Latex 转 Word 虽然latex 格式很方便,能够满足绝大部分的排版要求,但是在与人沟通的时候不可避免地需要用到其他格式文件,比 ...
- 基于jenkins自动打包并部署docker环境
一.实验环境 git 192.168.200.71 jenkins 192.168.200.72 docker 192.16 ...
- MYSQL 索引汇总
1.MySQL索引类型 先分以下类,MYQL有两大类索引:聚集索引和非聚集索引(只考虑mysql innodb) 聚集索引:在有主键的情况下,主键为聚集索引,其他都是非聚集索引 ...
- 用VC实现洪水攻击程序
本文为原创,如需转载,请注明作者和出处,谢谢! 一. 什么是洪水攻击 洪水之猛.势不可挡.如果将洪水比作对计算机的攻击,那大家可以想象得出,攻击是多的猛烈. 在安全领域所指的洪 ...
- Onedrive File Open Problem
在用Onenote 2016写笔记时,同步到Onedrive后就变成了Internet快捷方式,而且直接打开时会出现提示: 原因 用Onenote客户端在Onedrive上创建的笔记本是一种特殊文件, ...
- C++编程入门题目--No.2
题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高 于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提 成7.5%:20万到4 ...
- python模块之time and datetime
time # python3 # coding = utf-8 import time timestamp = time.time() print('timestamp:%s, type: %s' % ...