E. e-Government

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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

input
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序 + 树状数组的更多相关文章

  1. AC自动机fail树上dfs序建线段树+动态memset清空

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=4117 思路:https://blog.csdn.net/u013306830/article/detail ...

  2. 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)

    传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...

  3. 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2022  Solved: 1158[Submit][Sta ...

  4. CodeForces - 1207G :Indie Album(AC自动机 fail树上DFS)

    题意:有N个串,给出的形式是拼接给出,对于第i行:  (1,c)表示字符串i是单个字母c: (2,p,c)表示字符串i=在字符串p后面接上一个字母c. 然后给出M个提问,形式是(i,string).问 ...

  5. bzoj2434 fail树 + dfs序 + 树状数组

    https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...

  6. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  7. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  8. 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组

    题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...

  9. 【学习笔记】ac自动机&fail树

    定义 解决文本串和多个模式串匹配的问题: 本质是由多个模式串形成的一个字典树,由tie的意义知道:trie上的每一个节点都是一个模式串的前缀: 在trie上加入fail边,一个节点fail边指向这个节 ...

随机推荐

  1. 【视频处理】YUV与RGB格式转换

    YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...

  2. Date小技巧:set相关操作及应用_获取当前月(季度/年)的最后一天

    set操作还是有不少的,具体见 http://www.w3school.com.cn/jsref/jsref_obj_date.asp, 今天我就只说 setFullYear, setMonth, s ...

  3. kmdjs集成uglifyjs2打造极致的编程体验

    回顾 上篇文章大概展示了kmdjs0.1.x时期的编程范式: 如下面所示,可以直接依赖注入到function里, kmdjs.define('main',['util.bom','app.Ball', ...

  4. CentOS7安装NodeJS6.9

    1.下载 wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz 2.解压 tar -xJf node-v6.9.2-linu ...

  5. Android EventBus 3.0.0 使用总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...

  6. luke使用

    Luke介绍 Luke是一个方便的索引查看和诊断工具,可以访问Lucene构建的索引文件,显示和修改某些索引内容.能提供: 通过document编号或term浏览索引 查看document内容,可复制 ...

  7. 记录一次Quartz2D学习(六)

    (五)内主要讲到了一些图层操作 本次主要讲 图片的裁剪 6图片裁剪 6.1 图片裁剪 TIP: 可能会影响统一图层的显示 - (void)drawRect:(CGRect)rect { CGConte ...

  8. [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1

         Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...

  9. SQL Server 如何通过SQL语句定位SSRS中的具体报表

    在一些IT技术人员的推广.简单培训后,公司很多部门都有一些非IT技术人员参与开发各自需求的Reporting Service报表.原因很简单,罗列出来的原因大概有这样一些: IT部门的考量: 1:IT ...

  10. SSH批量部署服务

    SSH批量部署服务 1.1在NFS上作为中心分发服务器将私钥分发到其他服务器上 1.1.1NFS部署 [root@nfs-server ~]# useradd zhurui [root@nfs-ser ...