日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

luogu P3808 【模板】AC自动机(简单版)

發(fā)布時(shí)間:2025/7/25 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 luogu P3808 【模板】AC自动机(简单版) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

二次聯(lián)通門 :?luogu P3808 【模板】AC自動(dòng)機(jī)(簡單版)

?

?

?

?

/*luogu P3808 【模板】AC自動(dòng)機(jī)(簡單版)手速越來越快了10分鐘一個(gè)AC自動(dòng)機(jī)一遍過編譯 + 一邊AC感覺不錯(cuò)我也就做做板子題了。。*/ #include <iostream> #include <cstring> #include <cstdio> #include <queue>#define Max 1000009void read (int &now) {register char word = getchar ();for (now = 0; !isdigit (word); word = getchar ());for (; isdigit (word); now = now * 10 + word - '0', word = getchar ()); }struct Trie_Data {Trie_Data *child[26];Trie_Data *Fail;int Count;Trie_Data (){for (int i = 0; i < 26; i ++)this->child[i] = NULL;Fail = NULL;Count = 0;} };Trie_Data *Root; std :: queue <Trie_Data *>Queue;class AC_Type {public :void Insert (char *key){Trie_Data *now = Root;Trie_Data *pos;int Id;int Len = strlen (key);for (int i = 0; i < Len; i ++){Id = key[i] - 'a';if (now->child[Id] == NULL)now->child[Id] = new Trie_Data; now = now->child[Id];}now->Count ++;}void AC_Build (){Queue.push (Root); Trie_Data *now;Trie_Data *pos;while (!Queue.empty ()){pos = NULL;now = *&Queue.front ();Queue.pop ();for (int i = 0; i < 26; i ++){if (now->child[i] == NULL)continue;if (now == Root)now->child[i]->Fail = Root;else{pos = now->Fail;for (; pos; pos = pos->Fail)if (pos->child[i] != NULL){now->child[i]->Fail = pos->child[i];break;}if (pos == NULL)now->child[i]->Fail = Root;}Queue.push (now->child[i]); } }}int Query (char *key){int Answer = 0;int Len = strlen (key);int Id;Trie_Data *now = Root;for (int i = 0; i < Len; i ++){Id = key[i] - 'a';while (now->child[Id] == NULL && now != Root)now = now->Fail;now = now->child[Id];if (now == NULL)now = Root;Trie_Data *pos = now;for (; pos != Root && pos->Count >= 0; pos = pos->Fail){Answer += pos->Count;pos->Count = -1;}}return Answer;} };AC_Type Make;char line[Max];int main (int argc, char *argv[]) {int T, N;Root = new Trie_Data;for (read (N); N --; ){scanf ("%s", line);Make.Insert (line); }Make.AC_Build (); scanf ("%s", line);printf ("%d\n", Make.Query (line)); return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/ZlycerQan/p/7341480.html

總結(jié)

以上是生活随笔為你收集整理的luogu P3808 【模板】AC自动机(简单版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。