题意

维护一个字符串的集合$D$, 支持3种操作:

  1. 插入一个字符串$s$
  2. 删除一个字符串$s$
  3. 查询一个字符串$s$在$D$中作为子串出现的次数

强制在线

解法

AC自动机+二进制分组

二进制分组

二进制分组是一种用 (套用) 离线方法解决要求强制在线问题的分块技巧. 我第一次见到它是在2013年IOI国家集训队许昊然的论文<浅谈数据结构题的几个非经典解法>中. 满足修改操作对询问的贡献独立, 修改操作之间互不影响效果 (其实前后两句说的是同一件事) 的数据结构题, 都可以采用二进制分组算法.

原理

修改操作序列按二进制分组. 所谓"二进制", 指的是将长为$n$的修改序列按原顺序分成$k$组 (实际上是对时间分块), $k$为$n$的二进制表示中1的数目, 第$i$组的长度为第$i$个1的权重 (2的幂), 每组用一个数据结构维护. 例如, 长为10的操作序列将分成两组长度分别为 8 (1~8), 2 (9~10). 当修改序列的长度从$n$变成$n+1$时, 暴力重建变化的那些组, 不难看出需要重建的修改序列的总长度为lowbit($n+1$).

不难看出, 这个题目所涉及的操作与询问满足上述条件.

实现

二进制分组的框架:

设修改序列为vector<operation> s, 数据结构序列为 vector<structure> t.

  1. pop_back: 将过期的分组从队尾弹出
for(x=S.size(); x && lowbit(x)<lowbit(S.size()+1); t.pop_back(), x-=lowbit(x));
s.push_back(cur_op); // 将当前修改操作加进修改序列
  1. push_back: 将新建分组入队
structure cur;    // 初始化为空
for(int i=s.size()-lowbit(s.size()); i<s.size(); i++) // 0-indexed
cur.insert(s[i]);
t.push_back(cur);

Implementation

本题中除了用到二进制分组意外, 还有一个巧妙的转化:

分别维护插入和删操作形成的AC-自动机组 X, Y (即把删除操作也当作插入操作), 最后结果就是在X中查询的答案减去在Y中查询的答案.

#include <bits/stdc++.h>
using namespace std; const int N{1<<19}, M{1<<10};
typedef long long LL; vector<string> s; struct node{
int ch[26], fail, last, cnt;
bool f;
}; int lowbit(int x){
return x&-x;
} queue<int> que; struct AC{
using trie = vector<node>;
vector<trie> g; //group
vector<string> s; //string buffer void insert(const string &t){
for(int x=s.size(); x && lowbit(x)<lowbit(s.size()+1); g.pop_back(), x-=lowbit(x)); //error-prone
g.push_back({});
auto &cur=*g.rbegin();
cur.push_back({});
s.push_back(t); for(int i=s.size()-lowbit(s.size()); i<s.size(); i++){
int u=0;
for(auto &x:s[i]){
// int &v=cur[u].ch[x-'a']; //error
// do not use a reference to an object stored in a vector or any other dynamically allocated container,
// when it is under construction.
if(!cur[u].ch[x-'a']){
cur[u].ch[x-'a']=cur.size();
cur.push_back({});
}
u=cur[u].ch[x-'a'];
}
cur[u].cnt=1;
cur[u].f=true;
} for(int i=0; i<26; i++){
int u=cur[0].ch[i];
if(u) que.push(u);
} for(; que.size(); ){
int u=que.front();
que.pop();
for(int i=0; i<26; i++){
int &v=cur[u].ch[i]; //error-prone
if(v){ //v is a new node
que.push(v);
int &fail=cur[v].fail, &last=cur[v].last;
fail=cur[cur[u].fail].ch[i];
last=cur[fail].f ? fail : cur[fail].last;
cur[v].cnt+=cur[last].cnt;
// alternative: cur[v].cnt+=cur[fail].cnt;
}
else v=cur[cur[u].fail].ch[i];
}
}
} LL match(const string &t){
LL res=0;
for(auto &cur: g){
int u=0;
for(auto &x: t){ // no need to add a const before auto
u=cur[u].ch[x-'a'];
res+=cur[u].cnt;
}
}
return res;
}
}a, b; int main(){
int m, tail=0;
cin>>m;
string x;
for(int i=0, t; i<m; i++){
cin>>t>>x;
if(t==1){
a.insert(x);
}
else if(t==2){
b.insert(x);
}
else cout<<a.match(x)-b.match(x)<<endl;
}
}

代码中注释了我当时犯的一个不易察觉的错误.

Codeforces 710F String Set Quries的更多相关文章

  1. Codeforces 710F - String Set Queries(AC 自动机)

    题面传送门 题意:强制在线的 AC 自动机. \(n,\sum|s|\leq 3\times 10^5\) 如果不是强制在线那此题就是道 sb 题,加了强制在线就不那么 sb 了. 这里介绍两种做法: ...

  2. 【Codeforces 710F】String Set Queries

    Codeforces 710 F 思路:KMP学的还是不过关啊... 按照字符串的长度分类,如果长度大于\(\sqrt{n}\)的就扔到什么地方等待查询,否则就扔进trie里面. 对于查询,我们先在t ...

  3. 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 ...

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

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

  5. Codeforces 799D. String Game 二分

    D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...

  6. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  7. CodeForces 159c String Manipulation 1.0

    String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...

  8. CodeForces 779D. String Game(二分答案)

    题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...

  9. Codeforces 110B-Lucky String(技能)

    B. Lucky String time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 前端css框架SASS使用教程(转)

    一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...

  2. SAP CRM 使用Javascript触发SAP Server Event

    原文地址:How To Trigger SAP Server Event With Javascript 本文地址:http://www.cnblogs.com/hhelibeb/p/5977921. ...

  3. VS2012 Unit Test——Microsoft Fakes入门

    如题,本文主要作为在VS2012使用Fakes的入门示例,开发工具必须是VS2012或更高版本. 关于Fakes的MSDN地址:http://msdn.microsoft.com/en-us/libr ...

  4. 阶段一:用Handler和Message实现计时效果及其中一些疑问

    “阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 本来是打算继续做天气预报的优化的,但因为某些原因,我要先把之前做的小应用优化一下.所以今天就插播一下用Handle ...

  5. iOS App之间跳转

    1.先来看看效果,这里做了三个功能 从MyApp跳转到YourApp 从MyApp跳转到YourApp的指定页面 从YourApp使用跳转url的方式跳回MyApp 2.实现app之间的跳转需要注意两 ...

  6. SpringMVC + mybatis + Druid insert 数据库中文乱码,查询无乱码

    之前一直在pom文件配置的数据库连接url,很多配置都写在pom文件中导致配置文件太长 新项目将配置写到不同的文件夹中得properties文件中了 db.url直接复制的pom文件中的 p.p1 { ...

  7. 原创 C++之常量(二)

    4常量的内存分配 4.1应用程序的内存结构 一个由C++编译的应用程序,占用的内存可以划分为如下几个部分: 栈(stack).由编译器自动分配释放.存放函数参数和函数里的局部变量(又称自动变量).其操 ...

  8. asp.net mvc 之旅 —— 第六站 ActionFilter的应用及源码分析

    这篇文章我们开始看一下ActionFilter,从名字上其实就大概知道ActionFilter就是Action上的Filter,对吧,那么Action上的Filter大概有几个呢??? 这个问题其实还 ...

  9. mysql想要别人远程能连接

    mysql -u root -pvmware mysql>use mysql; mysql>update user set host = '%' where user = 'root'; ...

  10. Say goodbye to my photos&videos

    刚刚得知一个悲惨的消息:虽然2012已经过去了,但是世界末日并未过去.嗯,我不是来严肃的,我是来搞笑的.毕竟,我已经如此伤心了.中午结束考试,下午看了一半的电影然后躺室友的床上睡了一觉,醒来看到阿姨发 ...