Codeforces 163E. e-Government

AC自动机+树状数组改段求点。。。

给你一个S 支持删一个元素 把一个删的元素添加回来 询问特定串P的匹配次数

我们发现这个题目的flag要仔细的处理 否则添加删除会很麻烦

那么只在”确实是串结尾“的地方有一个flag好了(也就是说fail指向的那种不要了)

现在把P放进AC自动跑 每跑到一个位置i 我们求出P[1..i]的匹配次数(为了防止重复我们要求必须包含i这个字符)

其实 本质就是 P[1..i]的所有后缀中 有多少个∈S

这就很显然了 预处理fail树 询问根到特定点上的权值和

这个显然可以DFS序+树状数组

修改也是很显然的


  考虑把fail指针反向,由于ac自动机的每个节点均有唯一的fail指针,若是沿着fail指针往上走,显然都会走到root,所以反向之后显然是一棵树,不妨称之为fail树。

  fail树有什么特点呢?可以画个图试试,如果儿子节点出现过,那么他的祖先显然也会出现!因此,我们统计某个节点时,实际上就是统计在fail树中该节点到根的所有节点出现过的总次数。怎么统计?这不就是用dfs序维护树状数组吗?于是这题得以解决。每次寻找的时间复杂度为O(len*log)的级别。

E. e-Government
time limit per test
3 seconds
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 area1, 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.

Sample test(s)
input
7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa
output
6
4
3
6

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define ll long long
#define prt(k) cout<<#k"="<<k<<" "
inline void Max(int& a,int b) { if(a<b) a=b; }
const int M=1e6+7,N=1e5+8;
int ch[M][26],fail[M];
bool end[M];
int pos[N],node[N];
char str[M];
int Size,root;
int newnode() { memset(ch[Size],-1,sizeof ch[0]); end[Size++]=0; return Size-1; }
void AC_init() { Size=0; root=newnode(); }
void insert(char s[],int id)
{
    int n=strlen(s),u=root;
    for(int i=0;i<n;i++)
    {
        int& tmp=ch[u][s[i]-'a'];
        if(tmp==-1)tmp=newnode();
        u=tmp;
    }
    end[u]=1;
    node[id]=u;
}
void AC_build()
{
    queue<int> q;
    for(int i=0;i<26;i++)
    {
        int& tmp=ch[root][i];
        if(tmp==-1) tmp=root;
        else { fail[tmp]=root; q.push(tmp); }
    }
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<26;i++)
        {
            int& tmp=ch[u][i];
            if(tmp==-1) tmp=ch[fail[u]][i];
            else { fail[tmp]=ch[fail[u]][i]; q.push(tmp); }
        }
    }
}
#define left Left
#define right Right
int left[M],right[M];
vector<int> g[M];
int tot;
void dfs(int u)
{
    left[u]=++tot;
    for(int i=0;i<g[u].size();i++) dfs(g[u][i]);
    right[u]=tot;
}
/*********Segment Tree***********/
int tree[M<<2];
void pushup(int l,int r,int rt) {}
/************End Segment Tree******/
int lowbit(int x) {return x&-x; }
void add(int p,int x)
{
    for(int i=p;i>0;i-=lowbit(i)) tree[i]+=x;
}
void add(int l,int r,int x)
{
    add(r,x); if(l) add(l-1,-x);
}
int sum(int p)
{
    int ret=0;
    for(int i=p;i<=Size;i+=lowbit(i)) ret+=tree[i];
    return ret;
}
int query(char s[])
{
    int u=root,n=strlen(s),ret=0;
    for(int i=0;i<n;i++) { u=ch[u][s[i]-'a']; ret+=sum(left[u]); }
    return ret;
}
bool flag[N];
int toint(char s[])
{
    int ret=0;
    for(int i=0;s[i];i++) ret=ret*10+s[i]-'0';
    return ret;
}
int n,q;
int main()
{
    cin>>q>>n;  AC_init();
    memset(end,0,sizeof end);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str);
        insert(str,i);
    }
    AC_build();
   // for(int i=0;i<Size;i++) printf("fail[%d]=%d ",i,fail[i]);  putchar(10);
   // for(int i=1;i<=n;i++) printf("node[%d]=%d ",i,node[i]); putchar(10);
    for(int i=1;i<Size;i++) g[fail[i]].push_back(i);
    tot=0;
    dfs(root);
  //  for(int i=0;i<Size;i++) printf("left[%d]=%d  right[%d]=%d\n",i,left[i],i,right[i]);
    memset(flag,true,sizeof flag);
    memset(tree,0,sizeof tree);
    for(int i=1;i<Size;i++) if(end[i]) add(left[i],right[i],1);
    while(q--)
    {
        scanf("%s",str);
        char op=str[0];
        if(op=='?')
        {
            printf("%d\n",query(str+1));
        }
        if(op=='+')
        {
            int k=toint(str+1);
            if(flag[k]) continue;
            flag[k]=1;
            k=node[k];
            add(left[k],right[k],1);
        }
        if(op=='-')
        {
            int k=toint(str+1);
            if(!flag[k]) continue;
            flag[k]=0;
            k=node[k];
            add(left[k],right[k],-1);
        }
    }
}



你可能感兴趣的:(数据结构,算法,字符串,ACM,AC自动机)