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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AC自动机-HDU2222-模板题

發布時間:2023/11/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AC自动机-HDU2222-模板题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://acm.hdu.edu.cn/showproblem.php?pid=2222

一個AC自動機的模板題。用的kuangbin的模板,靜態建Trie樹。可能遇到MLE的情況要轉動態建樹。

?

AC自動機的講解看這里

http://blog.csdn.net/niushuai666/article/details/7002823

http://blog.csdn.net/mobius_strip/article/details/22549517

/*--------------------------------------------------------------------------------------*/ // Helica's header // Second Editions // 2015.11.7 // #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map>//debug function for a N*M array #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ {for(int j=0;j<(M);j++){\ printf("%d",G[i][j]);}printf("\n");} //debug function for int,float,double,etc. #define debug_var(X) cout<<#X"="<<X<<endl; /*--------------------------------------------------------------------------------------*/ using namespace std;int N,M,T;struct Trie {int next[500010][26],fail[500010],end[500010];int root,L;int newnode(){for(int i=0;i<26;i++) next[L][i] = -1;end[L++] = 0;return L-1;}void init(){L = 0;root = newnode();}void insert(char *s){int len = strlen(s);int now = root;for(int i=0;i<len;i++){if(next[now][s[i]-'a'] == -1)next[now][s[i]-'a'] = newnode();now = next[now][s[i]-'a'];}end[now]++;}void build(){queue <int> Q;fail[root] = root;for(int i=0;i<26;i++)if(next[root][i] == -1)next[root][i] = root;else{fail[next[root][i]] = root;Q.push(next[root][i]);}while(!Q.empty()){int now = Q.front();Q.pop();for(int i=0;i<26;i++){if(next[now][i] == -1)next[now][i] = next[fail[now]][i];else{fail[next[now][i]] = next[fail[now]][i];Q.push(next[now][i]);}}}}int query(char *s){int len = strlen(s);int now = root;int ans = 0;for(int i=0;i<len;i++){now = next[now][s[i]-'a'];int temp = now;while(temp != root){ans += end[temp];end[temp] = 0;temp = fail[temp];}}return ans;}void debug(){for(int i=0;i<L;i++){printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);for(int j=0;j<26;j++)printf("%2d",next[i][j]);printf("]\n");}} };char buf[1000100]; Trie ac;int main() {scanf("%d",&T);while(T--){ac.init();scanf("%d",&N);for(int i=0;i<N;i++){scanf("%s",buf);ac.insert(buf);}ac.build();//ac.debug();scanf("%s",buf);printf("%d\n",ac.query(buf));} }

?

?

第N次入門AC自動機。。。成功。。。

轉載于:https://www.cnblogs.com/helica/p/5037814.html

總結

以上是生活随笔為你收集整理的AC自动机-HDU2222-模板题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。