【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government
The best programmers of Embezzland compete to develop a part of the project called "e-Government" — the system of automated statistic collecting and press analysis.
We know that any of the k citizens can become a member of the Embezzland government. The citizens' surnames are a1, a2, ..., ak. All surnames are different. Initially all k citizens from this list are members of the government. The system should support the following options:
- Include citizen ai to the government.
- Exclude citizen ai from the government.
- Given a newspaper article text, calculate how politicized it is. To do this, for every active government member the system counts the number of times his surname occurs in the text as a substring. All occurrences are taken into consideration, including the intersecting ones. The degree of politicization of a text is defined as the sum of these values for all active government members.
Implement this system.
Input
The first line contains space-separated integers n and k (1 ≤ n, k ≤ 105) — the number of queries to the system and the number of potential government members.
Next k lines contain the surnames a1, a2, ..., ak, one per line. All surnames are pairwise different.
Next n lines contain queries to the system, one per line. Each query consists of a character that determines an operation and the operation argument, written consecutively without a space.
Operation "include in the government" corresponds to the character "+", operation "exclude" corresponds to "-". An argument of those operations is an integer between 1 and k — the index of the citizen involved in the operation. Any citizen can be included and excluded from the government an arbitrary number of times in any order. Including in the government a citizen who is already there or excluding the citizen who isn't there changes nothing.
The operation "calculate politicization" corresponds to character "?". Its argument is a text.
All strings — surnames and texts — are non-empty sequences of lowercase Latin letters. The total length of all surnames doesn't exceed106, the total length of all texts doesn't exceed 106.
Output
For any "calculate politicization" operation print on a separate line the degree of the politicization of the given text. Print nothing for other operations.
Examples
7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa
output
6
4
3
6
Solution
fail树的经典运用。
先建出fail树,然后用树状数组维护DFS序即可。
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define MAXN 1000100
int K,N,loc[MAXN],visit[MAXN];
struct EdgeNode{int next,to;}edge[MAXN<<];
int head[MAXN],cnt=;
inline void AddEdge(int u,int v) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}
inline void InsertEdge(int u,int v) {AddEdge(u,v); AddEdge(v,u);}
char S[MAXN];
namespace FailTree
{
int son[MAXN][],end[MAXN],sz=,fail[MAXN];
#define id(str) str-'a'+1
inline int Insert(int x,char str[])
{
int len=strlen(str+),now=;
for (int i=; i<=len; i++)
if (son[now][id(str[i])]) now=son[now][id(str[i])];
else son[now][id(str[i])]=++sz,now=sz;
end[now]=; loc[x]=now;
}
queue<int>q;
inline void Getfail()
{
q.push();
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=; i<=; i++)
if (son[now][i])
{
int fa=fail[now];
while (fa && !son[fa][i]) fa=fail[fa];
fail[son[now][i]]=fa? son[fa][i]:;
q.push(son[now][i]);
}
}
for (int i=; i<=sz; i++) InsertEdge(fail[i],i);
}
}
using namespace FailTree;
namespace Divide
{
int pl[MAXN],pr[MAXN],dfn,tree[MAXN<<];
inline void DFS(int now,int last)
{
pl[now]=++dfn;
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=last)
DFS(edge[i].to,now);
pr[now]=++dfn;
}
inline int lowbit(int x) {return x&-x;}
inline void Modify(int pos,int D) {for (int i=pos; i<=dfn; i+=lowbit(i)) tree[i]+=D;}
inline int Query(int pos) {int re=; for (int i=pos; i; i-=lowbit(i)) re+=tree[i]; return re;}
inline int Calc(char str[])
{
int len=strlen(str+),ans=,now=;
for (int i=; i<=len; i++)
{
while (now && !son[now][id(str[i])]) now=fail[now];
now=now? son[now][id(str[i])]:;
ans+=Query(pl[now]);
}
return ans;
}
inline void Change(int x,int D)
{
if (visit[x] && D>) return;
if (!visit[x] && D<) return;
visit[x]^=;
Modify(pl[loc[x]],D); Modify(pr[loc[x]],-D);
}
}
using namespace Divide;
int main()
{
scanf("%d%d",&K,&N);
for (int i=; i<=N; i++) scanf("%s",S+),Insert(i,S);
Getfail(); DFS(,);
for (int i=; i<=N; i++) Modify(pl[loc[i]],),Modify(pr[loc[i]],-),visit[i]=;
while (K--)
{
char opt=getchar(); int x;
while (opt!='+' && opt!='-' && opt!='?') opt=getchar();
switch (opt)
{
case '+' : scanf("%d",&x); Change(x,); break;
case '-' : scanf("%d",&x); Change(x,-); break;
case '?' : scanf("%s",S+); printf("%d\n",Calc(S)); break;
}
}
return ;
}
【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组的更多相关文章
- AC自动机fail树上dfs序建线段树+动态memset清空
题意:http://acm.hdu.edu.cn/showproblem.php?pid=4117 思路:https://blog.csdn.net/u013306830/article/detail ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2022 Solved: 1158[Submit][Sta ...
- CodeForces - 1207G :Indie Album(AC自动机 fail树上DFS)
题意:有N个串,给出的形式是拼接给出,对于第i行: (1,c)表示字符串i是单个字母c: (2,p,c)表示字符串i=在字符串p后面接上一个字母c. 然后给出M个提问,形式是(i,string).问 ...
- bzoj2434 fail树 + dfs序 + 树状数组
https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- 【学习笔记】ac自动机&fail树
定义 解决文本串和多个模式串匹配的问题: 本质是由多个模式串形成的一个字典树,由tie的意义知道:trie上的每一个节点都是一个模式串的前缀: 在trie上加入fail边,一个节点fail边指向这个节 ...
随机推荐
- java多态的理解
面向对象语言中的类有三个特征,封装.继承.多态.封装与继承很好理解,那什么是多态呢? 1.什么是多态? 多态的定义:指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同 ...
- Android 手机卫士7--黑名单拦截
1,黑名单数据库创建 三个字段(_id 自增长字段 phone 黑名单号码 mode 拦截类型) 创建表的sql语句 create table blacknumber (_id integer pri ...
- swift 如何在IOS应用图标上添加消息数
在应用图标右上角添加消息数提醒,可以很方便的告知用户该应用中有无新消息需要处理.下面用xcode 7.3.1来简要说明一下如何用swift语言进行此功能的实现. 1.修改 AppDelegate.sw ...
- ES6之const命令
一直以来以ecma为核心的js始终没有常量的概念,es6则弥补了这一个缺陷: const foo='foo'; foo='bar';//TypeError: Assignment to constan ...
- 使用GIT进行源码管理 —— 在VisualStudio中使用GIT
GIT作为源码管理的方式现在是越来越流行了,在VisualStudio 2012中,就通过插件的现实对GIT进行了官方支持,并且这个插件在VS2013中已经转正.本文在这里简单的介绍一下如何在Visu ...
- 如何用Github版本控制非Github库
Git的图形化客户端有很多,不同的人可能习惯用不同的客户端.本人更习惯于Github的客户端,因为上Github比较多,同步代码到Github用官方的客户端是最方便的,所以也就更习惯于使用Github ...
- gitflow以及git
git大概只会用常用的那几个命令,自己用的最多的也只是add commit push pull之类的,然后那天电话面试问我版本回退怎么办我都忘记了. 然后又看了看教程,此时想起来做项目的时候师兄说的g ...
- nodejs安装和环境部署
windows 下: 1. 下载windows平台nodejs环境安装包,百度一下nodejs官网,找到DOWNLOADS点击,找到Windows Installer 如果为64位电脑可以选择64位版 ...
- 微信app支付 ci框架做的
/** * 组合微信app支付 获得prepayid * @param int $order_num */ private function _wxpay_reques ...
- Python学习实践-----打印日历
使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...