zoukankan      html  css  js  c++  java
  • bzoj2754: [SCOI2012]喵星球上的点名

    传送门

    事实证明,我肉眼debug了两个晚上,还是不及对拍效率高。

    AC自动机写错了都毫不自觉的智障宸

    因为数据水,写的暴力

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    #include<map>
    #define For(i,a,b) for(int i=(a);i<=(b);i++)
    #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    const int N=400007;
    typedef long long LL;
    using namespace std;
    int n,m,a[N],ll[N],rr[N],ans2[N],cnt[N];
    
    template<typename T>void read(T &x)  {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    map<int,int>ch[N];
    #define IT map<int,int>::iterator
    int tot,rt,w[N],fail[N],ans[N];
    void insert(int ql,int qr) {
        if(!rt) rt=++tot;
        int x=rt;
        For(i,ql,qr) {
            int c=a[i];
            if(!ch[x][c]) ch[x][c]=++tot;
            x=ch[x][c];
        } 
        w[x]++;
    }
    
    queue<int>que;
    void get_fail() {
        que.push(rt);
        while(!que.empty()) {
            int x=que.front();
            que.pop();
            for(IT it=ch[x].begin();it!=ch[x].end();it++) {
                int y=it->second;
                if(x==rt) fail[y]=rt;
                else {
                    int z=fail[x];
                    for(;fail[z]&&!ch[z][it->first];z=fail[z]);
                    if(ch[z][it->first]) fail[y]=ch[z][it->first];
                    else fail[y]=rt;
                }
                que.push(y); 
            }
        }
    }
    
    #define pr pair<int,int>
    pr sta[N];
    int top;
    int qry(int ql,int qr) {
        int x=rt,rs=0;
        For(i,ql,qr) {
            int c=a[i];
            for(;fail[x]&&!ch[x][c];x=fail[x]);
            if(ch[x][c]) {
                x=ch[x][c]; 
                if(w[x]) {
                    rs+=w[x]; cnt[x]++;
                    sta[++top]=make_pair(x,w[x]);
                    w[x]=0;    
                }
                int y=fail[x];
                while(w[y]) {
                    rs+=w[y];
                    sta[++top]=make_pair(y,w[y]);
                    w[y]=0; cnt[y]++;
                    y=fail[y];
                }
            }
        }
        while(top) {
            pr tp=sta[top--];
            w[tp.first]=tp.second;
        }
        return rs;
    } 
    
    void calc(int ql,int qr) {
        int x=rt;
        For(i,ql,qr) {
            int c=a[i];
            x=ch[x][c];
        }
        printf("%d
    ",cnt[x]);
    }
    
    //#define DEBUG
    int main() {
    #ifdef DEBUG
        freopen("2754.in","r",stdin);
        freopen("2754.out","w",stdout);
    #endif
        read(n); read(m);
        For(i,1,n) {
            int len; read(len);
            ll[i]=a[0]+1;
            For(j,1,len) read(a[++a[0]]);
            a[++a[0]]=-1;
             read(len);
            For(j,1,len) read(a[++a[0]]);
            rr[i]=a[0];
        }
        For(i,1,m) {
            int len; read(len);
            ll[n+i]=a[0]+1;
            For(j,1,len) read(a[++a[0]]);
            rr[n+i]=a[0];
            insert(ll[n+i],rr[n+i]);
        }
        get_fail();
        For(i,1,n) 
            ans[i]=qry(ll[i],rr[i]);
        For(i,1,m)
            calc(ll[n+i],rr[n+i]);
        For(i,1,n-1) printf("%d ",ans[i]);
        printf("%d",ans[n]);
        return 0;
    }
    /*
    4 1
    1 2 1 2 
    4 3 1 1 9 6 6 9 1 10 7 9 
    4 8 1 9 9 6 9 9 7 9 3 10 
    6 8 9 2 1 1 4 1 4 
    1 9 
    5 8 9 1 5 2 
    4 2 10 7 6 
    2 5 1
    */
    View Code

     

  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8561793.html
Copyright © 2011-2022 走看看