字典树。

插入的时候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. Eclipse报错 due to restriction on required library C:/Java/jdk1.7.51/jre/lib/rt.jar 解决方案

    Eclipse报错 due to restriction on required library C:/Java/jdk1.6.0_10/jre/lib/rt.jar 解决方案 Eclipse 编译时 ...

  2. 如何清除jboss缓存

    要清除Jboss下的缓存,只要清除以下文件的所有文件就可以了:1.D:\JavaServer\jboss-4.2.2.GA\server\default\tmp2.D:\JavaServer\jbos ...

  3. passwd总结

    1.当前用户是root root用户修改密码 ,直接 passwd[不要输入当前用户密码] 如果修改其他用户密码,需要 passwd 用户名 如: passwd sc 短短的密码,如123也能通过,因 ...

  4. byte数组与int,long,short,byte转换 (转载)

    byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...

  5. css 重新学习系列(3)

    摘自:http://www.cnblogs.com/websugar/articles/2406416.html   十步图解CSS的Position CSS的position,我想做为一个Web制作 ...

  6. aX+bY+cZ=n(非负整数解存在性)

    题意: a*1234567+b*123456+c*1234=n 非负整数解得存在性. 题解: 看代码. #include<iostream> #include<cstdio> ...

  7. CodeForces#378--A, B , D--暴力出奇迹....

    A题 A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes in ...

  8. java 缓冲流

    english.txt The arrow missed the target. They rejected the union demand. Where does this road go to? ...

  9. [原创] web_custom_request 与 Viewstate

    在用loadrunner对.net编写的website进行性能测试时,经常会遇上一些hidden fields,例如,CSRFTOKEN.VIEWSTATE.EVENTVALIDATION等,而对于这 ...

  10. 2333: [SCOI2011]棘手的操作[写不出来]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...