【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边指向这个节 ...
随机推荐
- Singleton(单例模式)的一种实现 -- 基于【惰性】适用于【大对象】的一种生产实践
一.说明 本文中的代码类,在生产中使用了很长的时间,曾应用于多个企业多个项目实践中,其中也踩了不少坑,总结了一些适用的业务情景, 重要的事情说三遍: a.本代码类不是万能药,不要在业务情景中滥用! b ...
- js判断窗体或容器滚动条到底部
NO1---jquery判断窗体滚动条到底部 $(window).scroll(function () {if ($(window).scrollTop() >= $(document).hei ...
- JavaScript中数据类型转换总结
JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...
- WereWolf项目 Postmortem
WereWolf项目 Postmortem (博客园的MarkDown编辑器好像有些问题,编号都显示1..) 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描 ...
- js中的constructor
定义和用法 constructor 属性返回对创建此对象的 Date 函数的引用. 语法 object.constructor constructor属性不影响任何JavaScript的内部属性.in ...
- inotify+rsync实现实时同步部署
1.1.架构规划 1.1.1架构规划准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server-inotify-tools) 192.168.1.14 Cen ...
- mongodb高级应用
一. 高级查询 查询操作符 条件操作符:db.collection.find({"field":{$gt/$lt/$gte/$lte/$eq/$ne:value}}); 匹配所有 ...
- CSS3 基于关系的选择器
常见的基于关系的选择器 选择器 选择的元素 A E 元素A的任一后代元素E (后代节点指A的子节点,子节点的子节点,以此类推) A > E 元素A的任一子元素E(也就是直系后代) E:first ...
- url中的特殊符号含义
1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...
- 如何把select默认的小三角替换成自己的图片
不同的浏览器默认的select的选项图标是不同的,例如: 在chrome中,是这样的: 未点击时 点击时 在Firefox中是这样的: 未点击时 点击时 在IE9中是这样的: 未点击时 ...