Codeforces-163e:e-government (AC automaton +dfs sequence + Tree array)

Source: Internet
Author: User

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 government. The citizens ' surnames is a1, a2, ..., ak. 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 any surnames doesn ' t exceed 6, the total length of all texts doesn ' t exceed ten6.

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

Test Instructions: There are two different words, and n operations. First give m words, then n operation,

Operation 1, delete the word I (if it has been deleted, ignore it);

Action 2, add, and vice versa.

Action 3, given the string s, queries how many occurrences of the currently existing word in the string s (can be repeated).

idea: set up an AC automaton for M words, then the fail tree, and the DFS sequence for the fail tree.

Assuming that there is no fail tree and DFS sequence, only the fail pointer, I beg s to appear the number of times, s on the AC automaton running, for each current SI in the AC automaton now position, all up the number, indicating the string ending with I, the number of occurrences.

After setting up the fail tree, X's fail pointer is the father of X, so when fail appears, X also appears. That is, when X appears, the subtree will be + +, so +1,-1 on the tree array;

Get the DFS sequence, ask the string s, run on the AC automaton, accumulate the contribution of the tree array.

accuracy: because in the inquiry string AC automaton running above, I run the depth is the biggest, has contributed to it to use the fail tree and number array update, did not heavy does not leak.

#include <bits/stdc++.h>using namespacestd;Const intmaxn=1000010;intch[maxn][ -],cnt=0;//trie TreeintPOS[MAXN],ST[MAXN];//The location of the trie tree. intLaxt[maxn],next[maxn],to[maxn],tot;//Fail TreeintQ[maxn],fail[maxn],head,tail;//Fail Treeint inch[MAXN], out[Maxn],sum[maxn],times;//DFS sequenceCharC[MAXN];CharGetopt () {CharC=getchar (); while(c!='+'&&c!='-'&&c!='?') C=getchar ();returnC;}voidAddedge (intUintV) {Next[++tot]=laxt[u]; Laxt[u]=tot; to[tot]=v;}intInsert () {intL=strlen (c+1), now=0;  for(intI=1; i<=l;i++){        if(!ch[now][c[i]-'a']) ch[now][c[i]-'a']=++CNT; now=ch[now][c[i]-'a']; }   returnNow ;}voidBuildfail () { for(intI=0;i< -; i++){        if(ch[0][i]) q[++head]=ch[0][i],fail[ch[0][i]]=0; Elsech[0][i]=0; }     while(tail<head) {        intnow=q[++tail];  for(intI=0;i< -; i++){            if(Ch[now][i]) {q[++head]=ch[now][i]; fail[ch[now][i]]=Ch[fail[now]][i]; }            Elsech[now][i]=Ch[fail[now]][i]; }    }     for(intI=1; i<=cnt;i++) Addedge (fail[i],i);}voidDfsintu) {    inch[u]=++Times ;  for(intI=laxt[u];i;i=Next[i]) DFS (to[i]);  out[u]=Times ;}voidAddsum (intXintVal) { while(X<=times) {sum[x]+=val; x+= (-X) &x;}}intQueryintx) {intres=0; while(x) {res+=sum[x];x-= (-X) &x;}returnRes;}voidsolve () {intL=strlen (c+1), now=0, ans=0;  for(intI=1; i<=l;i++) { now=ch[now][c[i]-'a']; Ans+=query (inch[now]); } printf ("%d\n", ans);}intMain () {intn,m,x,i,j; scanf ("%d%d",&n,&l);  for(i=1; i<=m;i++) {St[i]=1; scanf ("%s", c+1); Pos[i]=Insert ();    } buildfail (); DFS (0);  for(i=1; i<=m;i++) {addsum (inch[Pos[i]],1); Addsum ( out[pos[i]]+1,-1); }     for(i=1; i<=n;i++){        Charopt=getopt (); if(opt=='?') {scanf ("%s", c+1);        Solve (); }        Else{scanf ("%d",&x); if(opt=='+'){                if(st[x]==1)Continue; st[x]=1; Addsum (inch[Pos[x]],1); Addsum ( out[pos[x]]+1,-1); }            Else {                if(st[x]==0)Continue; st[x]=0; Addsum (inch[pos[x]],-1); Addsum ( out[pos[x]]+1,1); }        }    }    return 0;}

Codeforces-163e:e-government (AC automaton +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.