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 exceed 106, 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

Copy
  1. 7 3
    a
    aa
    ab
    ?aaab
    -2
    ?aaab
    -3
    ?aaab
    +2
    ?aabbaa
output

Copy
  1. 6
    4
    3
    6

  1.  

题意:

给出n个字符串,表示n个人名,有两种操作:

?string ,统计字符串string中出现的属于城市居民的次数。

+id,把编号为id的人变为城市居民,如果已经是忽略。

-id,把编号为id的人变为不是城市居民,如果已经不是的话忽略。

现有m个操作,对于?输出结果。

思路:

我们在统计的时候其实就是沿着fail指针走,把所有的标记叠加起来,而fail指针构成了一棵fail树,所以我们在求当前节点的fail指针方向有多少个标记的时候不必一层层的fail上去了,对于每个点维护其到根的有效节点的个数即可,当更新某个点的时候,就相当于这个点的子树到根的有效节点的个数都发生了变化,将树形结构变成线性结构,在树状数组中更新即可。

 

参考代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define lowbit(x) (x&-x)
  5. #define maxn 1000010
  6. int n,m,tot;
  7. char s[maxn];
  8. vector<int> vec[maxn];
  9. int ch[maxn][],fail[maxn],val[maxn],last[maxn];
  10. int c[maxn],in[maxn],out[maxn],tim,id[maxn],use[maxn];
  11.  
  12. inline void Modify(int x,int num)
  13. {
  14. if(x==) return ;
  15. while(x<maxn)
  16. {
  17. c[x]+=num;
  18. x+=lowbit(x);
  19. }
  20. }
  21. inline void Add(int x,int y,int num)
  22. {
  23. Modify(x,num);Modify(y,-num);
  24. }
  25. inline int Query(int x)
  26. {
  27. int res=;
  28. while(x>)
  29. {
  30. res+=c[x];
  31. x-=lowbit(x);
  32. }
  33. return res;
  34. }
  35.  
  36. inline void Init()
  37. {
  38. tot=;tim=;
  39. memset(ch[],,sizeof(ch[]));
  40. memset(val,,sizeof(val));
  41. memset(use,,sizeof(use));
  42. }
  43. inline int idx(char c){ return c-'a';}
  44. inline void Insert(char*s,int x)
  45. {
  46. int u=,len=strlen(s);
  47. for(int i=;i<len;++i)
  48. {
  49. int c=idx(s[i]);
  50. if(!ch[u][c])
  51. {
  52. memset(ch[tot],,sizeof(ch[tot]));
  53. val[tot]=;
  54. ch[u][c]=tot++;
  55. }
  56. u=ch[u][c];
  57. }
  58. val[u]=x;
  59. id[x]=u;
  60. }
  61. inline void GetFail()
  62. {
  63. queue<int> q;
  64. fail[]=;
  65. for(int c=;c<;++c)
  66. {
  67. int u=ch[][c];
  68. if(u){ fail[u]=;q.push(u);last[u]=; }
  69. }
  70. //cout<<"cnt "<<cnt<<endl;
  71. while(!q.empty())
  72. {
  73. int r=q.front(); q.pop();
  74. vec[fail[r]].push_back(r);
  75. for(int c=;c<;++c)
  76. {
  77. int u=ch[r][c];
  78. if(!u){ch[r][c]=ch[fail[r]][c];continue;}
  79. q.push(u);
  80. int v=fail[r];
  81. fail[u]=ch[v][c];
  82. last[u] = val[fail[u]]?fail[u]:last[fail[u]];
  83. }
  84. }
  85. }
  86. inline void dfs(int u)
  87. {
  88. in[u]=++tim;
  89. for(int i=,len=vec[u].size();i<len;++i)
  90. dfs(vec[u][i]);
  91. out[u]=tim;
  92. }
  93.  
  94. inline void clac(int x,int num)
  95. {
  96. Add(in[id[x]],out[id[x]]+,num);
  97. }
  98.  
  99. inline void work()
  100. {
  101. ll ans=;
  102. int u=,len=strlen(s);
  103. for(int i=;i<len;++i)
  104. {
  105. int r=idx(s[i]);
  106. u=ch[u][r];
  107. ans+=Query(in[u]);
  108. }
  109. printf("%lld\n",ans);
  110. }
  111.  
  112. int main()
  113. {
  114. scanf("%d%d",&m,&n);
  115. Init();
  116. for(int i=;i<=n;++i)
  117. scanf("%s",s),Insert(s,i),use[i]=;
  118. GetFail();
  119. dfs();
  120.  
  121. for(int i=;i<=n;++i) clac(i,);
  122. while(m--)
  123. {
  124. scanf("%s",s);
  125. if(s[]=='?') work();
  126. else
  127. {
  128. int x;
  129. sscanf(s+,"%d",&x);
  130. if(use[x]&&s[]=='-')
  131. {
  132. use[x] = ;
  133. clac(x,-);
  134. }
  135. else if(!use[x]&&s[]=='+')
  136. {
  137. use[x] = ;
  138. clac(x,);
  139. }
  140. }
  141. }
  142.  
  143. return ;
  144. }
  145. /*
  146. 7 3
  147. a
  148. aa
  149. ab
  150. ?aaab
  151. -2
  152. ?aaab
  153. -3
  154. ?aaab
  155. +2
  156. ?aabbaa
  157. */

CoderForces 163E e-Government(AC自动机+树状数组维护fail树的dfs序)的更多相关文章

  1. BZOJ_2819_Nim_树状数组维护出栈入栈序

    BZOJ_2819_Nim_树状数组维护出栈入栈序 Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任 ...

  2. 【树状数组套主席树】带修改区间K大数

    P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...

  3. BZOJ 3881 [COCI2015]Divljak (Trie图+Fail树+树链的并+树状数组维护dfs序)

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

  4. POJ 3321 Apple Tree(后根遍历将树转化成序列,用树状数组维护)

    题意:一棵树,有很多分叉,每个分叉上最多有1个苹果. 给出n,接下来n-1行,每行u,v,表示分叉u,v之间有树枝相连.这里数据中u相当于树中的父节点,v相当于子节点. 给出两个操作: 1.C x  ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】

    任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...

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

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  8. 牛客练习赛52 B题【树状数组维护区间和{查询区间和,如果区间元素重复出现则计数一次}】补题ing

    [题目] 查询区间和,如果区间元素重复出现则计数一次. 链接:https://ac.nowcoder.com/acm/contest/1084/B [题解] 将询问按r排序,维护每个数最后出现的位置, ...

  9. HYSBZ - 3813 奇数国 欧拉函数+树状数组(线段树)

    HYSBZ - 3813奇数国 中文题,巨苟题,巨无敌苟!!首先是关于不相冲数,也就是互质数的处理,欧拉函数是可以求出互质数,但是这里的product非常大,最小都2100000,这是不可能实现的.所 ...

随机推荐

  1. centos7 openssh 7.9.1 升级

    由于项目构建时间比较长,近期安全检查发现openssh有漏洞.所以要升级openssh到7.9p1版本.由于ssh用于远程连接,所以要谨慎操作. 1. 依赖安装 OpenSSL版本:目前OpenSSH ...

  2. 如何在当前文件夹打开cmd(基于win10)

    如何在当前文件夹打开cmd(基于win10) 方法一: 1.先打开你要进入的文件夹 2.在标记的位置输入cmd,就可以进入当前文件的cmd 方法二: 1.打开你要进入的文件夹 2.通过shift + ...

  3. Mybatis动态SQL(where元素、set元素、if元素)

    Mybatis动态SQL(where元素.set元素.if元素) - where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句.而且,若语句的开头为“AND”或 ...

  4. ArcGIS API For Javascript :如何制作地图切换器

    大部分情况下我们开发会使用原生的地图切换器,由于每个项目的页面风格不同,业务场景不同,因此需要做一些样式不同的地图切换器. 首先可以照猫画虎,自己照着地图切换器的样式抄一个,或者看看主流的地图切换器都 ...

  5. gin索引优化实例1

    GIN(Generalized Inverted Index, 通用倒排索引) 是一个存储对(key, posting list)集合的索引结构,其中key是一个键值,而posting list 是一 ...

  6. 函数指针和成员函数指针有什么不同,反汇编带看清成员函数指针的本尊(gcc@x64平台)

    函数指针是什么,可能会答指向函数的指针. 成员函数指针是什么,答指向成员函数的指针. 成员函数指针和函数指针有什么不同? 虚函数指针和非虚成员函数指针有什么不同? 你真正了解成员函数指针了吗? 本篇带 ...

  7. 移动端vue项目的图片上传插件

    有一移动端项目,使用的vant-ui.可是vant自带的Uploader似乎不支持一次选择多张图片上传的功能. 于是乎:在https://www.npmjs.com/查找发现找到 vue-upload ...

  8. AppBoxFuture: 集成第三方Sql数据库

      框架设计之初是不准备支持第三方数据库的,但最近几个朋友都提到需要将旧的基于传统Sql数据库的应用迁移到框架内,主要是考虑到一方面目前框架内置的分布式数据库尚未完善,另一方面是希望能逐步迭代旧应用替 ...

  9. 2019-9-9:渗透测试,基础学习,phpmyadmin getshell方法,基于时间的盲注,基于报错的注入,笔记

    phpmyadmin getshell方法1,查看是否有导入导出设置 show global variables like '%secure-file-priv%';2,如果secure-file-p ...

  10. GitHub的高级搜索方式

    平时在学完一个知识后,需要写些 demo来进行练手,这个时候 GitHub就是最好不过的资源库了,以下整理了一些关于在 github 上面找项目的一些小技巧. 一.单条件使用 项目名称 仓库名称包含 ...