zoukankan      html  css  js  c++  java
  • CodeForces

    ou should process m queries over a set D of strings. Each query is one of three kinds:

    1. Add a string s to the set D. It is guaranteed that the string s was not added before.
    2. Delete a string s from the set D. It is guaranteed that the string s is in the set D.
    3. For the given string s find the number of occurrences of the strings from the set D. If some string p from D has several occurrences in s you should count all of them.

    Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.


    Input

    The first line contains integer m (1 ≤ m ≤ 3·105) — the number of queries.

    Each of the next m lines contains integer t (1 ≤ t ≤ 3) and nonempty string s — the kind of the query and the string to process. All strings consist of only lowercase English letters.

    The sum of lengths of all strings in the input will not exceed 3·105.

    Output

    For each query of the third kind print the only integer c — the desired number of occurrences in the string s.

    Examples
    Input
    5
    1 abc
    3 abcabc
    2 abc
    1 aba
    3 abababc
    Output
    2
    2
    Input
    10
    1 abc
    1 bcd
    1 abcd
    3 abcd
    2 abcd
    3 abcd
    2 bcd
    3 abcd
    2 abc
    3 abcd
    Output
    3
    2
    1
    0

    题意:三种操作,1,给集合S加串;2,删串;3,询问子串在S出现的个数。 强制在线。

    思路:如果不是强制在线,我们可以先对所有要加的串,建立AC自动机,然后用AC自动机+DFS序+线段树离线操作。

    这里要求在线,主要问题就是如何高效的维护AC自动机的fail,我们用二进制来分块;使得每个串最多合并log次。。。

    二进制分组:每次加串,就在尾巴建立一个字典树,集合大小为1; 如果集合大小和前面的相同,就合并到前面。  合并完后,对其建立AC自动机,得到fail和sum。

    对于强制在线的题,这是个不错的方式,复杂度O(NlogN),不过常数有点大。   我们也可以用分块的思想去做,O(NsqrtN)。

    (至于为什么网上都是加和删分别构造AC自动机,不太明白,感觉没必要。

    #include<bits/stdc++.h>
    #define ll long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=300010;
    char c[maxn]; int len;
    struct ACAM{
        int cnt,ch[maxn][26],fail[maxn],ac[maxn][26];
        int rt[maxn],num[maxn],sum[maxn],tot[maxn],T;
        ACAM(){
            cnt=0; T=0;
            rep(i,0,20) rt[i]=num[i]=0;
        }
        queue<int>que;
        void add(int &Now,int L,int opt)
        {
            if(!Now) Now=++cnt;
            if(L==len+1){ tot[Now]+=opt; return ;}
            add(ch[Now][c[L]-'a'],L+1,opt);
        }
        void build(int d) {
            que.push(d); fail[d]=d; sum[d]=0;
            for (int i=0;i<26;i++) ac[d][i]=d;
            while(!que.empty()) {
                int u=que.front();que.pop();
                sum[u]=sum[fail[u]]+tot[u];
               for (int i=0;i<26;i++) if (ch[u][i]) {
                   int v=ch[u][i];
                   que.push(v); fail[v]=ac[fail[u]][i]; ac[u][i]=v;
               } else ac[u][i]=ac[fail[u]][i];
            }
        }
        int merge(int x,int y)
        {
            if(!x||!y) return x^y;
            tot[x]+=tot[y];
            rep(i,0,25)
              ch[x][i]=merge(ch[x][i],ch[y][i]);
            return x;
        }
        void insert(int opt)
        {
            T++; add(rt[T],1,opt); num[T]=1;
            while(num[T]==num[T-1]){
                rt[T-1]=merge(rt[T-1],rt[T]);
                rt[T]=0; num[T-1]+=num[T];
                num[T]=0; T--;
            }
            build(rt[T]);
        }
        ll query()
        {
            ll res=0;
            rep(i,1,T) {
                if(!rt[i]) continue;
                int Now=rt[i];
                rep(j,1,len){
                    Now=ac[Now][c[j]-'a'];
                    res+=sum[Now];
                }
            }
            return res;
        }
    }a;
    int main()
    {
        int N,opt;
        scanf("%d",&N);
        rep(i,1,N){
            scanf("%d%s",&opt,c+1);
            len=strlen(c+1);
            if(opt==1) a.insert(1);
            else if(opt==2) a.insert(-1);
            else printf("%lld
    ",a.query()),fflush(stdout);
        }
        return 0;
    }

    两个,分别表示加和删:

    #include<bits/stdc++.h>
    #define ll long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=300010;
    char c[maxn]; int len;
    struct ACAM{
        int cnt,ch[maxn][26],fail[maxn],ac[maxn][26];
        int rt[maxn],num[maxn],sum[maxn],tot[maxn],T;
        ACAM(){
            cnt=0; T=0;
            rep(i,0,20) rt[i]=num[i]=0;
        }
        queue<int>que;
        void add(int &Now,int L)
        {
            if(!Now) Now=++cnt;
            if(L==len+1){ tot[Now]++; return ;}
            add(ch[Now][c[L]-'a'],L+1);
        }
        void build(int d) {
            que.push(d); fail[d]=d; sum[d]=0;
            for (int i=0;i<26;i++) ac[d][i]=d;
            while(!que.empty()) {
                int u=que.front();que.pop();
                sum[u]=sum[fail[u]]+tot[u];
               for (int i=0;i<26;i++) if (ch[u][i]) {
                   int v=ch[u][i];
                   que.push(v); fail[v]=ac[fail[u]][i]; ac[u][i]=v;
               } else ac[u][i]=ac[fail[u]][i];
            }
        }
        int merge(int x,int y)
        {
            if(!x||!y) return x^y;
            tot[x]+=tot[y];
            rep(i,0,25)
              ch[x][i]=merge(ch[x][i],ch[y][i]);
            return x;
        }
        void insert()
        {
            T++; add(rt[T],1); num[T]=1;
            while(num[T]==num[T-1]){
                rt[T-1]=merge(rt[T-1],rt[T]);
                rt[T]=0; num[T-1]+=num[T];
                num[T]=0; T--;
            }
            build(rt[T]);
        }
        ll query()
        {
            ll res=0;
            rep(i,1,T) {
                if(!rt[i]) continue;
                int Now=rt[i];
                rep(j,1,len){
                    Now=ac[Now][c[j]-'a'];
                    res+=sum[Now];
                }
            }
            return res;
        }
    }a,b;
    int main()
    {
        int N,opt;
        scanf("%d",&N);
        rep(i,1,N){
            scanf("%d%s",&opt,c+1);
            len=strlen(c+1);
            if(opt==1) a.insert();
            else if(opt==2) b.insert();
            else printf("%lld
    ",a.query()-b.query()),fflush(stdout);
        }
        return 0;
    }
    
     
    View Code
  • 相关阅读:
    偶对学习C#以及理解.Net平台的一些看法(二,Junior Bibliography)
    聊聊编程那些破事0.Prehistory
    偶对学习C#以及理解.Net平台的一些看法(一,Prerequisites)
    [转帖]c#.net常用函数列表
    一个编程小题目引发的思考(上)
    geoTools学习笔记001(简介)
    ArcGIS Server 10安装配置(JAVA)
    ARCGIS中label(标注)和Annotation(注记
    JSTL入门开发包详解
    基于C/S的网盘设计(JAVA) 网盘源码实现部分功能
  • 原文地址:https://www.cnblogs.com/hua-dong/p/10428669.html
Copyright © 2011-2022 走看看