LOJ10059(BZOJ3940)(Luogu3121)【USACO 2015 Feb. Gold】

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

LOJ10059

这题,乍一看,不就是LOJ10048嘛?
然后有N个单词。。多个KMP——AC自动机!!!
然后构造出这样的AC自动机:

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];

就可以写个栈一路走就好了。。

#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
using namespace std;
static char buf[1000000],*p1=buf,*p2=buf;
const int maxs=(1e5)+5;
int n,top=-1,m,que[maxs],lst[maxs];char s[maxs],stk[maxs];
struct Trie{
    int tot,c[maxs][30],End[maxs],nxt[maxs];
    void insert(){
        int rot=0,L=0;char ch=gt();while(!islower(ch)) ch=gt();
        while(islower(ch)) ++L,ch-='a',rot=(c[rot][ch]?c[rot][ch]:c[rot][ch]=++tot),ch=gt();
        End[rot]=L;
    }
}AC;
int read(){
    int ret=0;char ch=gt();
    while(ch<'0'||ch>'9') ch=gt();
    while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=gt();
    return ret;
}
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(){
    char ch=gt();
    while(islower(ch)) s[++m]=ch,ch=gt();
    n=read();
    for(int i=1;i<=n;i++) AC.insert();
    getnxt();
    for(int i=1,rot=0;i<=m;i++){
        lst[++top]=rot=AC.c[rot][(stk[top]=s[i])-'a'];
        if(AC.End[rot]) rot=lst[top-=AC.End[rot]];
    }
    stk[++top]='\n',stk[top+1]=0,fwrite(stk,1,top,stdout);
    return 0;
}

顺便说一句,洛谷真的有毒。。
看看这惨烈的一幕:
凉凉
然后,最后是这样A掉的:

#include<bits/stdc++.h>
#define gt() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++)
using namespace std;
static char buf[1000000],*p1=buf,*p2=buf;
const int maxs=(1e5)+5;
int n,top=-1,m,que[maxs],lst[maxs];char s[maxs],stk[maxs];
struct Trie{
    int tot,c[maxs][30],End[maxs],nxt[maxs];
    void insert(){
        int rot=0,L=0;char ch=gt();while(!islower(ch)) ch=gt();
        while(islower(ch)) ++L,ch-='a',rot=(c[rot][ch]?c[rot][ch]:c[rot][ch]=++tot),ch=gt();
        End[rot]=L;
    }
}AC;
int read(){
    int ret=0;char ch=gt();
    while(ch<'0'||ch>'9') ch=gt();
    while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=gt();
    return ret;
}
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(){
    char ch=gt();
    while(islower(ch)) s[++m]=ch,ch=gt();
    n=read();
    for(int i=1;i<=n;i++) AC.insert();
    getnxt();
    for(int i=1,rot=0;i<=m;i++){
        lst[++top]=rot=AC.c[rot][(stk[top]=s[i])-'a'];
        if(AC.End[rot]) rot=lst[top-=AC.End[rot]];
    }
    stk[++top]='\n',stk[top+1]=0,fwrite(stk-1,1,top,stdout);
    putchar('\n');
    return 0;
}

原来Luogu的字符数组下标从-1开始。。
哈哈哈

猜你喜欢

转载自blog.csdn.net/qq_42403731/article/details/82048342
今日推荐