洛谷 - P5357 AC自动机(二次加强版)


传送门:洛谷 - P5357

思路分析

第一种方法是建fail树,求子树大小
第二种方法是优化fail,只连单词末尾的点,叫last链

第一种AC代码


#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (int)(x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class t> inline void print(t x) {
        if (x < 0) putchar('-'), print(-x);
        else {
            if (x > 9) print(x / 10);
            putchar('0' + x % 10);
        }
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int INF = 0x3f3f3f3f;
const int N = 2e6+10;

int match[N];

struct AC_auto {
    struct T {
        int son[26],fail,sum;
        void clear() {
            mem(son,0);
            sum = fail = 0;
        }
    } tree[N];

    int cnt = 1;

    int newnode() {
        ++cnt;
        tree[cnt].clear();
        return cnt;
    }

    void init(){
        cnt = 1;
        tree[1].clear();
    }

    void ins(string s,int id) {
        int p = 1;
        for(auto i:s) {
            int c = i-'a';
            if(!tree[p].son[c]) tree[p].son[c] = newnode();
            p = tree[p].son[c];
        }
        match[id] = p;
    }

    void getfail() {
        for(int i=0; i<26; i++) tree[0].son[i] = 1;
        queue<int>q;
        q.push(1);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            for(int i=0; i<26; i++) {
                int v = tree[u].son[i];
                int ufail = tree[u].fail;
                if(v) {
                    tree[v].fail = tree[ufail].son[i];
                    q.push(v);
                } else tree[u].son[i] = tree[ufail].son[i];
            }
        }
    }
}AC;


vector<int>e[N];
int siz[N];

void dfs(int u) {
    for(auto v:e[u]) {
        dfs(v);
        siz[u]+=siz[v];
    }
}

signed main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int n;
    cin>>n;
    string s;
    for(int i=1; i<=n; i++) {

        cin>>s;
        AC.ins(s,i);
    }
    AC.getfail();
    cin>>s;
    int p = 1;
    for(auto i:s) {
        p = AC.tree[p].son[i-'a'];
        ++siz[p];
    }

    for(int i=2; i<=AC.cnt; i++) e[AC.tree[i].fail].pb(i);
    dfs(1);
    for(int i=1; i<=n; i++) cout<<siz[match[i]]<<endl;






    return 0;
}

第二种AC代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (int)(x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class t> inline void print(t x) {
        if (x < 0) putchar('-'), print(-x);
        else {
            if (x > 9) print(x / 10);
            putchar('0' + x % 10);
        }
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int INF = 0x3f3f3f3f;
const int N = 2e6+10;

int match[N];
int siz[N];

struct AC_auto {
    struct T {
        int son[26],fail,word,last;
        void clear() {
            mem(son,0);
            word = fail = last = 0;
        }
    } tree[N];

    int cnt = 1;

    int newnode() {
        ++cnt;
        tree[cnt].clear();
        return cnt;
    }

    void init(){
        cnt = 1;
        tree[1].clear();
        tree[1].word = 1;
    }

    void ins(string s,int id) {
        int p = 1;
        for(auto i:s) {
            int c = i-'a';
            if(!tree[p].son[c]) tree[p].son[c] = newnode();
            p = tree[p].son[c];
        }
        tree[p].word = id;
        match[id] = p;
    }

    void getfail() {
        for(int i=0; i<26; i++) tree[0].son[i] = 1;
        queue<int>q;
        q.push(1);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            int ufail = tree[u].fail;
            tree[u].last = tree[ufail].word>0?ufail:tree[ufail].last;
            for(int i=0; i<26; i++) {
                int v = tree[u].son[i];
                if(v) {
                    tree[v].fail = tree[ufail].son[i];
                    q.push(v);
                } else tree[u].son[i] = tree[ufail].son[i];
            }
        }
    }
    
    void query(string s){
        int p = 1;
        for(auto i:s){
            p = tree[p].son[i-'a'];
            for(int j = p;j!=1;j = tree[j].last){
                siz[tree[j].word]++;
            }
        }
        return ;
    }
}AC;


map<string,int>ID;
string s[N];
int tot;
 

signed main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int n;
    cin>>n;
    AC.init();
    for(int i=1; i<=n; i++) {

        cin>>s[i];
        if(!ID[s[i]]) ID[s[i]] = ++tot;
        AC.ins(s[i],ID[s[i]]);
    }
    AC.getfail();
    string x;
    cin>>x;
    AC.query(x);
    for(int i=1; i<=n; i++) cout<<siz[ID[s[i]]]<<endl;






    return 0;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}