LOJ10060(BZOJ3172)【TJOI 2013】

版权声明:发现蒟蒻ff_666,但转载请注明 https://blog.csdn.net/qq_42403731/article/details/82049348

LOJ10060

这题题目大意:给你N个单词(也是N段文章),求每个单词在这N段文章里的出现次数

然后随手写了个AC自动机——TLE了一个点。。

#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
#define pt(ch) (Top<1000000?St[Top++]=ch:(fwrite(St,1,1000000,stdout),St[(Top=0)++]=ch))
using namespace std;
int Top;static char St[1000000],buf[1000000],*p1=buf,*p2=buf;
const int maxn=205,maxs=(1e6)+5;
int n,Ans[maxs],L[maxn],que[maxs],pos[maxn];char s[maxn][maxs];
struct Trie{
    int tot,c[maxs][30],nxt[maxs];
    void insert(int x){
        int rot=0;char ch=gt();while(!islower(ch)) ch=gt();
        while(islower(ch)) s[x][++L[x]]=(ch-='a'),rot=(c[rot][ch]?c[rot][ch]:c[rot][ch]=++tot),ch=gt();
        pos[x]=rot;
    }
}AC;
void write(int x){if(x>9) write(x/10);pt(x%10+'0');}
void getnxt(){
    int hed=0,tal=0,x;
    for(int i=0;i<26;i++) if(AC.c[0][i]) que[++tal]=AC.c[0][i];
    while(hed!=tal){
        x=que[++hed];
        for(int i=0;i<26;i++)
          if(AC.c[x][i]) AC.nxt[que[++tal]=AC.c[x][i]]=AC.c[AC.nxt[x]][i];
            else AC.c[x][i]=AC.c[AC.nxt[x]][i];
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) AC.insert(i);
    getnxt();
    for(int j=1;j<=n;j++)
        for(int i=1,rot=0;i<=L[j];i++){
            rot=AC.c[rot][s[j][i]];
            for(int t=rot;t;t=AC.nxt[t]) Ans[t]++;
        }
    for(int i=1;i<=n;i++) write(Ans[pos[i]]),pt('\n');
    fwrite(St,1,Top,stdout);
    return 0;
}

因为匹配太慢了,怎么办呢?
LOJ的特别好处——可以面向数据编程。。
——我*,竟然这么多老长的相同串。。一个相同串处理一次!
然后就玄学过了LOJ和BZOJ。。 O ( )

#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
#define pt(ch) (Top<1000000?St[Top++]=ch:(fwrite(St,1,1000000,stdout),St[(Top=0)++]=ch))
using namespace std;
int Top;static char St[1000000],buf[1000000],*p1=buf,*p2=buf;
const int maxn=205,maxs=(1e6)+5;
int n,Ans[maxs],L[maxn],que[maxs],pos[maxn];char s[maxn][maxs];bool vis[maxs];
struct Trie{
    int tot,c[maxs][30],nxt[maxs],End[maxs];
    void insert(int x){
        int rot=0;char ch=gt();while(!islower(ch)) ch=gt();
        while(islower(ch)) s[x][++L[x]]=(ch-='a'),rot=(c[rot][ch]?c[rot][ch]:c[rot][ch]=++tot),ch=gt();
        pos[x]=rot,End[rot]++;
    }
}AC;
void write(int x){if(x>9) write(x/10);pt(x%10+'0');}
void getnxt(){
    int hed=0,tal=0,x;
    for(int i=0;i<26;i++) if(AC.c[0][i]) que[++tal]=AC.c[0][i];
    while(hed!=tal){
        x=que[++hed];
        for(int i=0;i<26;i++)
          if(AC.c[x][i]) AC.nxt[que[++tal]=AC.c[x][i]]=AC.c[AC.nxt[x]][i];
            else AC.c[x][i]=AC.c[AC.nxt[x]][i];
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) AC.insert(i);
    getnxt();
    for(int j=1;j<=n;j++)if(!vis[pos[j]]){//做一次
        vis[pos[j]]=1;
        for(int i=1,rot=0;i<=L[j];i++){
            rot=AC.c[rot][s[j][i]];
            for(int t=rot;t;t=AC.nxt[t]) Ans[t]+=AC.End[pos[j]];//一起叠加
        }
    }
    for(int i=1;i<=n;i++) write(Ans[pos[i]]),pt('\n');
    fwrite(St,1,Top,stdout);
    return 0;
}

接下来继续思考:
AC自动机是KMP的“合体”,所以构成的fail树也符合KMP的性质
——即最长的公共前后缀
也就是说当前节点[x]满足了,nxt[x]也一定满足
况且这题还是对自己匹配,那么直接在Trie上利用fail树刷树形DP不就好了吗!

F [ n x t [ x ] ] + = F [ x ]
那直接在BFS构造AC自动机后再累加一趟答案不就OK了!
复杂度线性: O ( | S | )

#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
#define pt(ch) (Top<1000000?St[Top++]=ch:(fwrite(St,1,1000000,stdout),St[(Top=0)++]=ch))
using namespace std;
int Top;static char St[1000000],buf[1000000],*p1=buf,*p2=buf;
const int maxn=205,maxs=(1e6)+5;
int n,Ans[maxs],L[maxn],que[maxs],pos[maxn];char s[maxn][maxs];
struct Trie{
    int tot,c[maxs][30],nxt[maxs];
    void insert(int x){
        int rot=0;char ch=gt();while(!islower(ch)) ch=gt();
        while(islower(ch)) s[x][++L[x]]=(ch-='a'),Ans[rot=(c[rot][ch]?c[rot][ch]:c[rot][ch]=++tot)]++,ch=gt();
        pos[x]=rot;
    }
}AC;
void write(int x){if(x>9) write(x/10);pt(x%10+'0');}
void getnxt(){
    int hed=0,tal=0,x;
    for(int i=0;i<26;i++) if(AC.c[0][i]) que[++tal]=AC.c[0][i];
    while(hed!=tal){
        x=que[++hed];
        for(int i=0;i<26;i++)
          if(AC.c[x][i]) AC.nxt[que[++tal]=AC.c[x][i]]=AC.c[AC.nxt[x]][i];
            else AC.c[x][i]=AC.c[AC.nxt[x]][i];
    }
    for(int i=tal;i;i--) Ans[AC.nxt[que[i]]]+=Ans[que[i]];//神奇的树形DP。。QWQ
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) AC.insert(i);
    getnxt();
    for(int i=1;i<=n;i++) write(Ans[pos[i]]),pt('\n');
    fwrite(St,1,Top,stdout);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42403731/article/details/82049348