"codeforces163e" e-government ac automaton fail tree + DFS sequence + Tree array

Source: Internet
Author: User

E. E-governmenttime limit per test:1 SecondMemory limit per test:MegabytesInputStandard InputOutputStandard Output

The best programmers of Embezzland compete to develop a part of the project called "E-government"-the system of Automate D statistic collecting and press analysis.

We know that any of The  K  citizens can become a member of the Embezzland Gove Rnment. The citizens ' surnames Are  a 1, a 2, ..., a K . All surnames is different. Initially All  K  citizens from this list is 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 to politicized it is. To does this, for every active government member the system counts the number of times he surname occurs in the text as a s Ubstring. All occurrences is 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 ≤10 5)-the Number of queries to the system and the number of potential government members.

Next k lines contain the surnames a1, a2, ..., a K, one per line. All surnames is pairwise different.

Next n lines contain queries to the system, one per line. Each query consists of a character this 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 are an integer between 1 and K -the Index of the citizen involved in the Operation. Any citizen can is included and excluded from the government a arbitrary number of times in any order. Including in the Government a citizen who's already there or excluding the citizen who isn ' t there changes nothing.

The operation "Calculate politicization" corresponds to character "?". It 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 exceed6, the total length of all texts doesn ' t exceed 6.

Output

For any "calculate politicization" operation print in a separate line the degree of the politicization of the given text. Print nothing for the other operations.

Examples input
3 ·
A
Aa
Ab
? aaab
-2
? aaab
-3
? aaab
+2
? Aabbaa

Output

6
4
3
6

Solution

The classic use of the fail tree.

The fail tree is built first, and then the DFS sequence is maintained using a tree-like array.

Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespacestd;#defineMAXN 1000100intK,N,LOC[MAXN],VISIT[MAXN];structedgenode{intNext,to;} edge[maxn<<1];intHead[maxn],cnt=1; inlinevoidAddedge (intUintV) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;} InlinevoidInsertedge (intUintv) {Addedge (u,v); Addedge (v,u);}CharS[MAXN];namespacefailtree{intson[maxn][ -],end[maxn],sz=1, FAIL[MAXN];#defineID (str) str-' a ' +1inlineintInsert (intXCharstr[]) {        intLen=strlen (str+1), now=1;  for(intI=1; i<=len; i++)            if(Son[now][id (Str[i])) now=Son[now][id (Str[i]); ElseSon[now][id (Str[i])]=++sz,now=sz; End[now]=1; loc[x]=Now ; } Queue<int>P; InlinevoidGetfail () {Q.push (1);  while(!Q.empty ()) {                intnow=Q.front (); Q.pop ();  for(intI=1; i<= -; i++)                    if(Son[now][i]) {intHasFail[now];  while(FA &&!son[fa][i]) fa=FAIL[FA]; Fail[son[now][i]]=fa? Son[fa][i]:1;                        Q.push (Son[now][i]); }            }         for(intI=1; i<=sz; i++) Insertedge (fail[i],i); }}using namespaceFailtree;namespacedivide{intpl[maxn],pr[maxn],dfn,tree[maxn<<1]; InlinevoidDFS (intNowintLast ) {Pl[now]=++DFN;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last )        DFS (Edge[i].to,now); Pr[now]=++DFN; } InlineintLowbit (intx) {returnx&-x;} InlinevoidModify (intPosintD) { for(intI=pos; i<=dfn; I+=lowbit (i)) tree[i]+=D;} InlineintQuery (intPOS) {intRe=0; for(intI=pos; I I-=lowbit (i)) re+=tree[i];returnre;} InlineintCalc (Charstr[]) {        intLen=strlen (str+1), ans=0, now=1;  for(intI=1; i<=len; i++)            {                 while(Now &&!son[now][id (Str[i])) now=Fail[now]; now=now? Son[now][id (Str[i]):1; Ans+=Query (Pl[now]); }        returnans; } InlinevoidChange (intXintD) {if(Visit[x] && d>0)return; if(!visit[x] && d<0)return; VISIT[X]^=1; Modify (PL[LOC[X]],D); Modify (Pr[loc[x]),-D); }}using namespaceDivide;intMain () {scanf ("%d%d",&k,&N);  for(intI=1; i<=n; i++) scanf ("%s", s+1), Insert (i,s); Getfail (); DFS (1,0);  for(intI=1; i<=n; i++) Modify (Pl[loc[i]),1), Modify (pr[loc[i]],-1), visit[i]=1;  while(k--)        {            CharOpt=getchar ();intx;  while(opt!='+'&& opt!='-'&& opt!='?') opt=GetChar (); Switch(opt) { Case '+': scanf ("%d", &x); Change (x,1); Break;  Case '-': scanf ("%d", &x); Change (x,-1); Break;  Case '?': scanf ("%s", s+1); printf"%d\n", Calc (S)); Break; }        }    return 0;}

"codeforces163e" e-government ac automaton fail tree + DFS sequence + Tree array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.