洛谷 - P3796 AC自动机(加强版)


传送门:洛谷 - P3796

思路分析

模板

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 = 1e6+10;

struct node {
    string s;
    int num;
    int id;
} a[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];
        }
        tree[p].sum = id;
    }

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

    void query(string s) {
        int ans = 0;
        int p = 1;
        for(auto i:s) {
            p = tree[p].son[i-'a'];
            for(int j = p; j!=1 && tree[j].sum!=-1; j = tree[j].fail) {
                a[tree[j].sum].num++;
            }
        }
    }
}AC;



signed main() {

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

    int n;
    while(cin>>n && n) {
        AC.init();
        string s;
        for(int i=1; i<=n; i++) {
            cin>>s;
            AC.ins(s,i);
            a[i] = {s,0,i};
        }
        AC.getfail();
        cin>>s;
        AC.query(s);
        sort(a+1,a+1+n,[](node x,node y) {
            if(x.num == y.num) return x.id<y.id;
            return x.num>y.num;
        });
        int ma = a[1].num;
        cout<<ma<<endl;
        for(int i=1; i<=n; i++) {
            if(a[i].num == ma) {
                cout<<a[i].s<<endl;
            } else {
                break;
            }
        }

    }





    return 0;
}



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