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

Posted hua-dong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)相关的知识,希望对你有一定的参考价值。

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
7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa
Output
6
4
3
6

题意:有M个不同的单词,和N个操作。先给出M个单词,然后N操作,

           操作1,删去第i个单词(如果已经删了,则忽略);

           操作2,添加,亦然。

           操作3,给出字符串S,查询当前存在的单词在字符串S种出现了多少次(可以重复统计)。

思路:对M个单词建立AC自动机,然后是fail树,对fail树求dfs序。

           假设没有求fail树和dfs序,只有fail指针,我求S出现次数的时候,S在AC自动机上跑,对于每一个当前Si在AC自动机的Now位置,都向上累加个数,表示以i为结尾的字符串,出现了多少次。

           建立了fail树后,x的fail指针是x的爸爸,那么fail出现的时候,x也出现。即x出现的时候,子树都会++;所以在树状数组上+1,-1;

           得到dfs序,询问串S时,在AC自动机上面跑,累加树状数组的贡献。

准确性:因为在询问串AC自动机上面跑的时候,我跑的深度是最大的,对它有贡献的都利用fail树和数状数组更新了,做到了不重不漏。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000010;
int ch[maxn][26],cnt=0; //trie树 
int pos[maxn],st[maxn]; //在trie树的位置。 
int Laxt[maxn],Next[maxn],To[maxn],tot; //fail树 
int q[maxn],fail[maxn],head,tail; //fail树 
int in[maxn],out[maxn],sum[maxn],times;//dfs序 
char c[maxn];
char getopt() { char c=getchar(); while(c!=+&&c!=-&&c!=?) c=getchar(); return c;}
void addedge(int u,int v){ Next[++tot]=Laxt[u]; Laxt[u]=tot; To[tot]=v; }
int insert()
{
    int L=strlen(c+1),Now=0;
    for(int i=1;i<=L;i++){
        if(!ch[Now][c[i]-a]) ch[Now][c[i]-a]=++cnt;
        Now=ch[Now][c[i]-a];
    }   return Now;
}
void buildfail()
{
    for(int i=0;i<26;i++){
        if(ch[0][i]) q[++head]=ch[0][i],fail[ch[0][i]]=0;
        else ch[0][i]=0;
    }
    while(tail<head){
        int Now=q[++tail];
        for(int i=0;i<26;i++){
            if(ch[Now][i]) {
                q[++head]=ch[Now][i]; fail[ch[Now][i]]=ch[fail[Now]][i];
            }
            else ch[Now][i]=ch[fail[Now]][i];
        }
    }
    for(int i=1;i<=cnt;i++) addedge(fail[i],i);
}
void dfs(int u)
{
    in[u]=++times;
    for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
    out[u]=times;
}
void addsum(int x,int val){ while(x<=times){ sum[x]+=val; x+=(-x)&x;}}
int query(int x){ int res=0;while(x){res+=sum[x];x-=(-x)&x;}return res;}
void solve()
{
    int L=strlen(c+1),Now=0,ans=0;
    for(int i=1;i<=L;i++){
        Now=ch[Now][c[i]-a];
        ans+=query(in[Now]);    
    }
    printf("%d
",ans);
}
int main()
{
    int N,M,x,i,j;
    scanf("%d%d",&N,&M);
    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(in[pos[i]],1);
        addsum(out[pos[i]]+1,-1);
    }
    for(i=1;i<=N;i++){
        char opt=getopt();
        if(opt==?){
           scanf("%s",c+1);
           solve();
        }
        else{
            scanf("%d",&x);
            if(opt==+){
                if(st[x]==1) continue;st[x]=1;
                addsum(in[pos[x]],1);
                addsum(out[pos[x]]+1,-1);    
            }
            else {
                if(st[x]==0) continue; st[x]=0;
                addsum(in[pos[x]],-1);
                addsum(out[pos[x]]+1,1);
            }
        }
    }
    return 0;
}

 

以上是关于CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces_729_E

Codeforces_496_E

codeforces-1328E-Tree Queries

Codeforces Round #603 (Div. 2)E

Codeforces-1196E

Educational Codeforces Round 32 E