CoderForces 163E e-Government(AC自动机+树状数组维护fail树的dfs序)
1 second
256 megabytes
standard input
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.
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 exceed 106, the total length of all texts doesn't exceed 106.
For any "calculate politicization" operation print on a separate line the degree of the politicization of the given text. Print nothing for other operations.
7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa
6
4
3
6
题意:
给出n个字符串,表示n个人名,有两种操作:
?string ,统计字符串string中出现的属于城市居民的次数。
+id,把编号为id的人变为城市居民,如果已经是忽略。
-id,把编号为id的人变为不是城市居民,如果已经不是的话忽略。
现有m个操作,对于?输出结果。
思路:
我们在统计的时候其实就是沿着fail指针走,把所有的标记叠加起来,而fail指针构成了一棵fail树,所以我们在求当前节点的fail指针方向有多少个标记的时候不必一层层的fail上去了,对于每个点维护其到根的有效节点的个数即可,当更新某个点的时候,就相当于这个点的子树到根的有效节点的个数都发生了变化,将树形结构变成线性结构,在树状数组中更新即可。
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lowbit(x) (x&-x)
#define maxn 1000010
int n,m,tot;
char s[maxn];
vector<int> vec[maxn];
int ch[maxn][],fail[maxn],val[maxn],last[maxn];
int c[maxn],in[maxn],out[maxn],tim,id[maxn],use[maxn]; inline void Modify(int x,int num)
{
if(x==) return ;
while(x<maxn)
{
c[x]+=num;
x+=lowbit(x);
}
}
inline void Add(int x,int y,int num)
{
Modify(x,num);Modify(y,-num);
}
inline int Query(int x)
{
int res=;
while(x>)
{
res+=c[x];
x-=lowbit(x);
}
return res;
} inline void Init()
{
tot=;tim=;
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
memset(use,,sizeof(use));
}
inline int idx(char c){ return c-'a';}
inline void Insert(char*s,int x)
{
int u=,len=strlen(s);
for(int i=;i<len;++i)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[tot],,sizeof(ch[tot]));
val[tot]=;
ch[u][c]=tot++;
}
u=ch[u][c];
}
val[u]=x;
id[x]=u;
}
inline void GetFail()
{
queue<int> q;
fail[]=;
for(int c=;c<;++c)
{
int u=ch[][c];
if(u){ fail[u]=;q.push(u);last[u]=; }
}
//cout<<"cnt "<<cnt<<endl;
while(!q.empty())
{
int r=q.front(); q.pop();
vec[fail[r]].push_back(r);
for(int c=;c<;++c)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
int v=fail[r];
fail[u]=ch[v][c];
last[u] = val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
inline void dfs(int u)
{
in[u]=++tim;
for(int i=,len=vec[u].size();i<len;++i)
dfs(vec[u][i]);
out[u]=tim;
} inline void clac(int x,int num)
{
Add(in[id[x]],out[id[x]]+,num);
} inline void work()
{
ll ans=;
int u=,len=strlen(s);
for(int i=;i<len;++i)
{
int r=idx(s[i]);
u=ch[u][r];
ans+=Query(in[u]);
}
printf("%lld\n",ans);
} int main()
{
scanf("%d%d",&m,&n);
Init();
for(int i=;i<=n;++i)
scanf("%s",s),Insert(s,i),use[i]=;
GetFail();
dfs(); for(int i=;i<=n;++i) clac(i,);
while(m--)
{
scanf("%s",s);
if(s[]=='?') work();
else
{
int x;
sscanf(s+,"%d",&x);
if(use[x]&&s[]=='-')
{
use[x] = ;
clac(x,-);
}
else if(!use[x]&&s[]=='+')
{
use[x] = ;
clac(x,);
}
}
} return ;
}
/*
7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa
*/
CoderForces 163E e-Government(AC自动机+树状数组维护fail树的dfs序)的更多相关文章
- BZOJ_2819_Nim_树状数组维护出栈入栈序
BZOJ_2819_Nim_树状数组维护出栈入栈序 Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任 ...
- 【树状数组套主席树】带修改区间K大数
P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...
- BZOJ 3881 [COCI2015]Divljak (Trie图+Fail树+树链的并+树状数组维护dfs序)
题目大意: Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)
题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】
题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- 牛客练习赛52 B题【树状数组维护区间和{查询区间和,如果区间元素重复出现则计数一次}】补题ing
[题目] 查询区间和,如果区间元素重复出现则计数一次. 链接:https://ac.nowcoder.com/acm/contest/1084/B [题解] 将询问按r排序,维护每个数最后出现的位置, ...
- HYSBZ - 3813 奇数国 欧拉函数+树状数组(线段树)
HYSBZ - 3813奇数国 中文题,巨苟题,巨无敌苟!!首先是关于不相冲数,也就是互质数的处理,欧拉函数是可以求出互质数,但是这里的product非常大,最小都2100000,这是不可能实现的.所 ...
随机推荐
- GitHub 发布了官方 App,还打算冰封你的代码一千年
11 月 13 日,GitHub Universe 2019 开发者大会上,公布了大量新功能,包括发布 GitHub 移动版.GitHub Actions 和 Packages 正式版上市.重新设计了 ...
- Class文件结构全面解析(上)
什么是Class文件? 在Java刚刚诞生的时候就提出了一个非常著名的口号:"一次编写,到处运行.(Write Once,Run Anywhere)".为了实现平台无关性,各种不同 ...
- ASP.NET Core 1.0: API的输入参数
Web API是需要接受参数的,譬如,通常用于创建数据的POST method需要接受输入数据,而用于GET method也需要接受一些可选参数,譬如:为了性能起见,控制返回数据的数量是至关重要的. ...
- pat 1058 A+B in Hogwarts(20 分)
1058 A+B in Hogwarts(20 分) If you are a fan of Harry Potter, you would know the world of magic has i ...
- MySQL 5.7 安装教程(Win 10)
MySQL5.7 下载 官网下载(不推荐使用):https://dev.mysql.com/downloads/mysql/ 清华镜像站下载(推荐):https://mirrors.tuna.tsin ...
- Centos上通过shell脚本备份数据库
#!/bin/bash ds=`` list=`date +%Y`/`date +%m` dname="callme" eval "mkdir -p $list" ...
- 十一、设备初始化(ADK4.0)
1.1 首先初始化连接库 sinkConnectionInit();à ConnectionInitEx2(); theCm.task.handler = connectionBluesta ...
- Mac安装和卸载Mysql
目录 一.安装 二.环境变量 2.1 MySQL服务的启停和状态的查看 三.启动 四.初始化设置 4.1 退出sql界面 五.配置 5.1 检测修改结果 一.安装 第一步:打开网址,https://w ...
- mysql--时区问题(时间差8个小时?修改Mysql 时区)
发现评论时间比本地时间晚8小时,原因:mysql默认时区选择了CST 解决办法: Ubuntu系统环境下: 1.检查mysql系统时区 进入mysql:mysql -u root -p mysql&g ...
- wordpress小程序安装教程
推荐服务器特价优惠注册即可购买,1G双核一年只要88,真的是白菜价格,点击下面图片即可进入购买地址. 开源小程序发布一段时间了,很多人最近咨询了关于小程序的教程,实在太忙了,抽空写个基本的安装教程. ...