【AC自動機】AC自動機模板,ac自動機模板

來源:互聯網
上載者:User

【AC自動機】AC自動機模板,ac自動機模板

感謝 小白菜菜菜 的更正,讓我可以擁有更簡潔的模板!

參考:AC自動機總結

代碼如下:

#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace std;const int N = 5e5+5, M = 1e6+5;struct Trie {    Trie *next[26];    Trie *fail;    int val;}tree[N];queue <Trie *> q;int idx[128];class ac_auto {    private:        int nxt;        Trie *root;    public:        ac_auto() {            nxt = 0;            root = add();        }        Trie *add() {            memset(&tree[nxt], 0, sizeof(Trie));            return &tree[nxt++];        }        void insert(char *s) {            Trie *rt = root;            int len = strlen(s);            for(int i = 0; i < len; i++) {                int c = idx[s[i]+0];                if(!rt->next[c])                    rt->next[c] = add();                rt = rt->next[c];            }            rt->val = 1;//rt->val++;用於統計        }        void get_f() {            queue <Trie *> q;            q.push(root);            root->fail = NULL;            while(!q.empty()) {                Trie *u = q.front(); q.pop();                for(int c = 0; c < 26; c++) {                    if(u->next[c]) {                        Trie *f = u->fail;                        while(f && !f->next[c]) f = f->fail;                        if(!f) u->next[c]->fail = root;                        else u->next[c]->fail = f->next[c];                        u->next[c]->val |= u->next[c]->fail->val;//狀態合并,根據具體題意,不能用於統計                        q.push(u->next[c]);                    }                    else if(u == root) u->next[c] = root;                    else u->next[c] = u->fail->next[c];                }            }        }        int match(char *s) {            Trie *rt = root;            int len = strlen(s), ret = 0;            for(int i = 0; i < len; i++) {                int c = idx[s[i]+0];                rt = rt->next[c];                Trie *p = rt;                while(p && p->val) {                    ret += p->val;                    //p->val = 0;統計的時候要清零                    p = p->fail;                }            }            return ret;        }};void haxi(){    for(int i = 0; i < 26; i++)         idx['a'+i] = i;}int main(){    haxi();    int T, m;    char str[M];    scanf("%d", &T);    while(T--) {        ac_auto ac;        scanf("%d", &m);        while(m--) {            scanf("%s", str);            ac.insert(str);        }        ac.get_f();        scanf("%s", str);        int ans = ac.match(str);        printf("%d\n", ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.