【Codeforces 163E】E-Government

题目描述

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.

Sample test(s)
Input

7 3
a
aa
ab
?aaab
-2
?aaab
-3
?aaab
+2
?aabbaa

Output

6
4
3
6

题目大意

初始给定n(<=100000)个串,总长不超过1000000,每个串初始时都是白色,
有k个操作,每次操作可以将一个串反色或给定一个串询问在这个串中所有黑色串出现次数之和。询问串总长不超过1000000

题解

AC自动机+fail树+dfs序+树状数组,其实还是很容易想到的

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <ctime>

using namespace std;

#define rep(i,l,r) for(i=l;i<=r;i++)
#define ser(i,r,l) for(i=r;i>=l;i--)
#define INF 1000005
#define inf 1000000007

typedef long long ll;
priority_queue<int >QwQ;

int tot,top=1,last=0,n,Q,ans=0,l=0,cnt=0;
int t[INF][30],fail[INF],id[INF],col[INF],q[INF],c[INF<<1],to[INF<<1],h[INF<<1],s[INF],L[INF],R[INF];
char s1[INF];
int read()
{
    int k=0,f=1;
    char ch;
    while(ch<'0' || ch>'9'){
      if(ch=='-')f=-1;
      ch=getchar();
    }
    while(ch>='0' && ch<='9')k=(k<<1)+(k<<3)+ch-'0',ch=getchar();
    return k*f;
}
int lowbit(int x){
    return x&(-x);
}
void hah(int x,int y)
{
    to[++l]=y,h[l]=s[x],s[x]=l;
}
void add(int x,int val)
{
    int i,j,k;
    for(i=x;i<=cnt;i+=lowbit(i))c[i]+=val;
}
int get_sum(int x)
{
    int i,j,k=0;
    for(i=x;i;i-=lowbit(i))k+=c[i];
    return k;
}
void Insert(int x)
{
    int i,j,k,l1,now=1;
    scanf("%s",s1);
    l1=strlen(s1);
    rep(i,0,l1-1){
        k=s1[i]-'a'+1;
        if(!t[now][k])t[now][k]=++tot;
        now=t[now][k];
    }
    col[x]=1,id[x]=now;
}
void build_fail()
{
    int i,j,k,p;
    q[0]=1,fail[1]=0;
    while(top>last){
        k=q[last++];
        rep(i,1,26)
         if(t[k][i]){
            p=fail[k];
            while(!t[p][i])p=fail[p];
            fail[t[k][i]]=t[p][i];
            q[top++]=t[k][i];
         }
    }
}
void dfs(int x)
{
    int i,j,k;
    L[x]=++cnt;
    for(i=s[x];i;i=h[i])
     dfs(to[i]);
    R[x]=++cnt;
}
void init()
{
    int i,j,k,now,p,x,l1;
    tot=1;
    rep(i,1,26)t[0][i]=1;
    Q=read(),n=read();
    rep(i,1,n)Insert(i);
    build_fail();
    rep(i,1,tot)hah(fail[i],i);
    dfs(0);
    rep(i,1,n)add(L[id[i]],1),add(R[id[i]]+1,-1);
    while(Q--){
        scanf("%s",s1);
        l1=strlen(s1);
        if(s1[0]=='?'){
            now=1,ans=0;
            rep(i,1,l1-1){
                k=s1[i]-'a'+1;
                while(!t[now][k])now=fail[now];
                now=t[now][k];
                ans+=get_sum(L[now]);
            }
            printf("%d\n",ans);
        }
        else if(s1[0]=='-'){
            sscanf(s1+1,"%d",&x);
            if(col[x]){
                col[x]=0;
                add(L[id[x]],-1),add(R[id[x]]+1,1);
            }
        }
        else{
            sscanf(s1+1,"%d",&x);
            if(!col[x]){
                col[x]=1;
                add(L[id[x]],1),add(R[id[x]]+1,-1);
            }
        }
    }
}
void work()
{
    int i,j,k;
}
int main()
{
    init();
    work();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值