字典树。

插入的时候update一下节点出现的次数。

delete的时候,先把前缀之后的全删了。然后看前缀最后一个节点出现了几次,然后前缀上每个节点的次数都减去这个次数。

前缀从上到下再检查一遍,如果出现了次数为0的节点,也删去。

#include <stdio.h>
#include <math.h>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<algorithm>
using namespace std; struct Node
{
int val;
int f[];
} node[]; int root,n;
char op[], s[];
int tot; int newnode()
{
++tot;
node[tot].val=;
for(int i=; i<; i++) node[tot].f[i]=-;
return tot;
} void Insert()
{
int p=root;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-) node[p].f[s[i]-'a']=newnode();
p=node[p].f[s[i]-'a'];
node[p].val++;
}
} void Delete()
{
int p=;
bool Find=;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-)
{
Find=;
break;
}
p=node[p].f[s[i]-'a'];
}
if(Find==) return; for(int i=; i<; i++) node[p].f[i]=-; int Val=node[p].val; p=root;
for(int i=; s[i]; i++)
{
p=node[p].f[s[i]-'a'];
node[p].val=node[p].val-Val;
} p=;
for(int i=; s[i]; i++)
{
int tmp=node[p].f[s[i]-'a'];
if(node[tmp].val==)
{
node[p].f[s[i]-'a']=-;
break;
}
p=node[p].f[s[i]-'a'];
}
} void Search()
{
int p=root; bool flag=;
for(int i=; s[i]; i++)
{
if(node[p].f[s[i]-'a']==-)
{
flag=;
break;
}
p=node[p].f[s[i]-'a'];
} if(flag) printf("No\n");
else printf("Yes\n");
} int main()
{
while(~scanf("%d",&n))
{
tot=; root=;
for(int i=; i<; i++) node[root].f[i]=-;
for(int i=; i<=n; i++)
{
scanf("%s%s",op,s);
if(strcmp(op,"insert")==) Insert();
else if(strcmp(op,"delete")==) Delete();
else if(strcmp(op,"search")==) Search();
}
}
return ;
}

2016"百度之星" - 资格赛(Astar Round1) Problem C的更多相关文章

  1. HDU 5688:2016"百度之星" - 资格赛 Problem D

    原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others)    ...

  2. HDU 5686:2016"百度之星" - 资格赛 Problem B

    原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others)    ...

  3. HDU 5685:2016"百度之星" - 资格赛 Problem A

    原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others)    ...

  4. 2016百度之星 资格赛ABCDE

    看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...

  5. 2016"百度之星" - 资格赛(Astar Round1) Problem E

    简单模拟题,耐心写就能过. #include <stdio.h> #include <math.h> #include<cstring> #include<c ...

  6. 2016"百度之星" - 资格赛(Astar Round1) Problem D

    排个序,map直接搞. #include <stdio.h> #include <math.h> #include<cstring> #include<cma ...

  7. 2016"百度之星" - 资格赛(Astar Round1) Problem B

    规律题,斐波那契数列,数据有毒,0的时候输出换行.会爆longlong,写个大数模板或者Java搞. import java.io.BufferedInputStream; import java.m ...

  8. 2016"百度之星" - 资格赛(Astar Round1) Problem A

    保存前缀乘,询问的时候输出c[ri]/c[li-1]即可,因为是除法,所以计算一下c[li-1]的逆元. #include <stdio.h> #include <math.h> ...

  9. 2016"百度之星" - 资格赛(Astar Round1)

    逆元 1001 Problem A 求前缀哈希和逆元 #include <bits/stdc++.h> typedef long long ll; const int MOD = 9973 ...

随机推荐

  1. process lasso 优化原理

    <星际争霸2:虚空之遗>很多玩家的CPU性能并不低,但是在星际2中的表现就总会出现掉帧的情况,那么应该如何提升CPU的性能就成了玩家关注的话题,下面小编就为大家带来星际争霸2虚空之遗cpu ...

  2. PHP实现队列的原理

    关于的队列的介绍,我这里就不多讲了,随便百度一下都很多 用过laravel框架的童鞋都知道其自带队列功能,之前我很费解,PHP只是一个脚本,有超时机制 为什么能不停的去执行队列呢? 带着这个问题,在网 ...

  3. centos 6.5常见lib库安装

    在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现. # yum install -y gcc gdb strace g ...

  4. Socks

    Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  5. runtime基础知识

    看到一篇不错的runtime方面博客: 引言 相信很多同学都听过运行时,但是我相信还是有很多同学不了解什么是运行时,到底在项目开发中怎么用?什么时候适合使用?想想我们的项目中,到底在哪里使用过运行时呢 ...

  6. Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)

    转载自:http://www.hangge.com/blog/cache/detail_1085.html UITextField.UITextView组件系统原生就支持文字的复制,但有时我们需要让其 ...

  7. UITableView使用中的一些刁专问题总结

    tableview中cell的系统分隔线问题(分隔线顶满或者缩短) //tableview代理方法,设置系统cell的分隔线 -(void)tableView:(UITableView *)table ...

  8. overflow:hidden

    超出之后隐藏,比如有一个div,高度和宽度都是100像素,当里面的内容很多,div里撑不下时,如果设置overflow:hidden,就会把超出的内容隐藏掉不显示

  9. HDU 5323 DFS

    DFS Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissio ...

  10. 6--OC--封装 继承 多态

    OC中的类的三大特性类: 继承,封装,多态 一. 封装 封装就是对类中的一些字段,方法进行保护,不被外界所访问到,有一种权限的控制功能,这样我们在定义类的时候,哪些字段和方法不想暴露出去,哪些字段和方 ...