F. String Set Queries

time limit per test:3 seconds
memory limit per test:768 megabytes
input:standard input
output:standard output

You should process m queries over a set D of strings. Each query is one of three kinds:

  1. Add a string s to the set D. It is guaranteed that the string s was not added before.
  2. Delete a string s from the set D. It is guaranteed that the string s is in the set D.
  3. For the given string s find the number of occurrences of the strings from the set D. If some string p from D has several occurrences in s you should count all of them.

Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Javalanguages after each writing in your program.

Input

The first line contains integer m (1 ≤ m ≤ 3·105) — the number of queries.

Each of the next m lines contains integer t (1 ≤ t ≤ 3) and nonempty string s — the kind of the query and the string to process. All strings consist of only lowercase English letters.

The sum of lengths of all strings in the input will not exceed 3·105.

Output

For each query of the third kind print the only integer c — the desired number of occurrences in the string s.

Examples

input
5
1 abc
3 abcabc
2 abc
1 aba
3 abababc
output
2
2
input
10
1 abc
1 bcd
1 abcd
3 abcd
2 abcd
3 abcd
2 bcd
3 abcd
2 abc
3 abcd

output

3
2
1
0

Solution

题目大意:给M个操作 1.插入一个串S  2.删除一个串S  3.给出一个串S,询问之前存在(插入后未被删除的)的串在S中出现的次数。

查询操作,显然是AC自动机随便做的,但是AC自动机不支持插入和删除。

不过可以考虑建多个AC自动机,查询时就全跑一边,这样显然是可以的,不过这样的复杂度明显是$O(N^{2})$的毫无意义。

但是建多个AC自动机是不可避免的了,不过即使这样时间复杂度完全可以从$O(N^{2})$优化下去,只需要优化建出的AC自动机数量就可以对总复杂度起到优化。

fail指针并不能快速合并,所以,只能暴力拆+暴力合,这种暴力拆解的合并,和启发式合并类似。

所以我们对这么多AC自动机按二进制分组,建出$logN$个AC自动机,这样时间复杂度就得到大幅优化。

这样实现起来其实也非常简单,用$root[i]$记录这个AC自动机的Trie树的根,顺带记录一下每个$root$的$size$,当当前的$root$的$size$和它前面的那个相同,就将他们合并,暴力拆小入大。

这样证明也和启发式合并一样,因为每个AC自动机最多被拆合$logN$次,所以复杂度是$O(NlogN)$

至于删除,比较麻烦的方法是在AC自动机上跑,然后把跑到的那个串的end标记-1。

但是,转化一下,我们所有删除的也按照上述方法处理建AC自动机,这样每次查询答案相减一下即可。
自己在写的时候有部分问题(常数优化)没及时注意到:
1.因为每次建AC自动机的时候,可能会进行合并,完全可以先进行合并再求一次fail指针,这样常数优化效果还是不错的。
2.在合并的时候,注意一下一边不存在时特判的写法,这里写的好坏,效率差距特别大!
优秀的写法:

自己愚蠢的写法:

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define MAXN 300010
int Q,opt,len,now;
char str[MAXN];
namespace ACMachine
{
struct Trie{int son[MAXN][],fail[MAXN],end[MAXN],tim[MAXN],sz;};
struct ACM
{
#define ch str[i]-'a'+1
int root[MAXN],cnt,size[MAXN];
Trie trie;
inline void Insert(int rt)
{
size[rt]++; now=rt;
for (int i=; i<=len; i++)
if (trie.son[now][ch]) now=trie.son[now][ch];
else trie.son[now][ch]=++trie.sz,now=trie.sz;
trie.end[now]=;
}
inline void Buildfail(int x)
{
queue<int>q;
q.push(x);
while (!q.empty())
{
now=q.front(); q.pop();
for (int i=; i<=; i++)
if (trie.son[now][i])
{
int fa=trie.fail[now];
while (fa && !trie.son[fa][i]) fa=trie.fail[fa];
trie.fail[trie.son[now][i]]=fa? trie.son[fa][i] : x;
trie.tim[trie.son[now][i]]=trie.end[trie.son[now][i]]+trie.tim[trie.fail[trie.son[now][i]]];
q.push(trie.son[now][i]);
}
}
}
inline int Merge(int ls,int rs)
{
if (!ls || !rs) return (ls+rs);
int rt=ls; if (!rt) return rt;
trie.end[rt]=trie.end[ls]|trie.end[rs];
for (int i=; i<=; i++)
trie.son[rt][i]=Merge(trie.son[ls][i],trie.son[rs][i]);
return rt;
}
inline int Debug()
{
int re=; now=root[cnt];
for (int i=; i<=len; i++)
{
while (now && !trie.son[now][ch]) now=trie.fail[now];
now=trie.son[now][ch];
re+=trie.tim[now];
}
printf("%d\n",re);
}
inline void MakeTrie()
{
root[++cnt]=++trie.sz;
Insert(root[cnt]);
while (cnt && size[cnt]==size[cnt-])
root[cnt-]=Merge(root[cnt-],root[cnt]),
size[cnt-]+=size[cnt],size[cnt]=,cnt--;
Buildfail(root[cnt]);
}
inline int Query()
{
int re=;
for (int r=; r<=cnt; r++)
for (int i=,now=root[r]; i<=len; i++)
{
while (now && !trie.son[now][ch]) now=trie.fail[now];
now=now? trie.son[now][ch] : root[r];
re+=trie.tim[now];
}
return re;
}
}In,Out;
}
using namespace ACMachine;
int main()
{
scanf("%d",&Q);
while (Q--)
{
scanf("%d%s",&opt,str+); len=strlen(str+);
switch (opt)
{
case : In.MakeTrie(); break;
case : Out.MakeTrie(); break;
case : printf("%d\n",In.Query()-Out.Query()); fflush(stdout); break;
}
}
return ;
}

一开始写丑了,无限卡常....CF迟迟case18TLE....

【Codeforces710F】String Set Queries (强制在线)AC自动机 + 二进制分组的更多相关文章

  1. CodeForces 710F 强制在线AC自动机

    题目链接:http://codeforces.com/contest/710/problem/F 题意:维护一个集合,集合要求满足三种操作. 1 str:向集合插入字符串str(保证不会插入之前已经插 ...

  2. CF710F-String Set Queries【AC自动机,二进制分组】

    正题 题目链接:https://www.luogu.com.cn/problem/CF710F 题目大意 \(T\)次操作 往集合中加入一个字符串 往集合中删除一个字符串 给出一个模式串求出现的集合里 ...

  3. GRE Words Revenge AC自动机 二进制分组

    GRE Words Revenge 题意和思路都和上一篇差不多. 有一个区别就是需要移动字符串.关于这个字符串,可以用3次reverse来转换, 前面部分翻转一下, 后面部分翻转一下, 最后整个串翻转 ...

  4. CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)

    ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...

  5. hdu_4787_GRE Words Revenge(在线AC自动机)

    题目链接:hdu_4787_GRE Words Revenge 题意: 总共有n个操作,2种操作.每行读入一个字符串. 1.如果字符串以+开头,此为单词(即模式串,不考虑重复) 2.如果字符串以?开头 ...

  6. Codeforces963C Frequency of String 【字符串】【AC自动机】

    题目大意: 给一个串s和很多模式串,对每个模式串求s的一个最短的子串使得这个子串中包含至少k个该模式串. 题目分析: 均摊分析,有sqrt(n)种长度不同的模式串,所以有关的串只有msqrt(n)种. ...

  7. 题解-Codeforces710F String Set Queries

    咕了好久没更博客,最近得知可以去冬眠营玩耍,还可以搭顺风车回广州过年 (最近做到的比较有意思的题目:bzoj3958.hihocoder1419) Problem Codeforces-710F--洛 ...

  8. ZOJ3784 String of Infinity 高大上的AC自动机 数据原来这么水啊!不算输入输出只有5-7行

    找给定s集合里面word全部是同一个字符的,这样的word有几个,如果数量<m就yes,否则就no.#include<iostream> #include<cstring> ...

  9. AC自动机

    AC自动机,全称Aho-Corasick自动机.如果没记错的话好像就是前缀自动机. 其实AC自动机就是KMP上树的产物.理解了KMP,那AC自动机应该也是很好理解的. 与KMP类似,AC自动机也是扔一 ...

随机推荐

  1. 【原】iOS动态性(四):一行代码实现iOS序列化与反序列化(runtime)

    为取得更好的排版效果,本文同样发布在简书上,强烈建议跳转到[1]http://www.jianshu.com/p/fed1dcb1ac9f 一.变量声明 为便于下文讨论,提前创建父类Biology以及 ...

  2. 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

  3. 显示转换explicit和隐式转换implicit

    用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...

  4. MS SQL 错误:无法绑定由多个部分组成的标识符 "xxxxx"

    今天有个同事问我一个SQL问题,觉得有点意思,虽然能很快定位并解决问题,但是就是有种说不清道不明的感觉.因为不能解释清楚(很多是建立在假设上),顺便记录一下,希望有清楚原理的人能解答一二. 原SQL语 ...

  5. Why Do We Need a Data Warehouse?

    https://dwbi1.wordpress.com/2012/12/03/why-do-we-need-a-data-warehouse/ 经常有人来质疑数据仓库的价值,为什么我们需要花费一年多的 ...

  6. PHP实现堆排序

    经验 工作了,面试我工作这家公司时被技术面打击得不行,因为自己的数据结构等基础学得实在太差,虽然原来是想做设计师的说...不过看在PHP写得还凑合的份上能来实习了,但还是决心恶补一下基础. 其实自己之 ...

  7. 使用 shell 脚本实现 LANMP 一键安装

    使用 shell 脚本来实现 LANMP 系统的一键安装.使用的操作系统是 CentOS 6 ,不区分 32 位和 64 位,要求机器可以连通互联网.支持 LAMP 和 LNMP ,MySQL 支持 ...

  8. MyBlog

    2016-06-25 一直想在搭建一个自己的blog.说来惭愧,cnblogs中的个人博客虽笔耕不辍,但杂乱无章.然而之前总是嘴上说说.现在,终于要行动起来了. 初步计划: 思路: django + ...

  9. 【Bootstrap Demo】入门例子创建

    本文简单介绍下如何来使用 Bootstrap,通过引入 Bootstrap,来实现一个最基本的入门例子. 在前一篇博文[Bootstrap]1.初识Bootstrap 基础之上,我们完全可以更加方便快 ...

  10. zlib压缩一个文件为gzip格式

    网上有很多针对zlib的总结,但是很零散,自己经过总结,实现了用zlib压缩一个文件为gzip格式,似的可以直接使用winr工具解压. 具体方法是使用zlib的deflate系列函数,将buffer压 ...