多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > HDU 2222 Keywords Search (初學(xué)AC自動(dòng)機(jī))

HDU 2222 Keywords Search (初學(xué)AC自動(dòng)機(jī))

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-03-28 08:17:03 閱讀次數(shù):2534次

我是通過(guò)http://wenku.baidu.com/view/4e70ccc38bd63186bcebbcb9.html的第2篇學(xué)會(huì)的

這篇也總結(jié)的很好,附帶很多經(jīng)典的習(xí)題http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

這是bin神的總結(jié):http://www.cnblogs.com/kuangbin/p/3164106.html


這是kss的板子:http://paste.ubuntu.com/10499866/

這個(gè)板子用了tire圖的優(yōu)化,即不需要失配回溯,效力大大提高


本題代碼:

普通模版:

//826MS 28308K #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; #define MAXN 500500 #define M 1001000 char s[55]; char desc[M]; struct node{ node *next[26]; node *fail; int cnt; }tire[MAXN],*que[MAXN],*root; struct AC { int L,head,tail; node *createnode(){ memset(tire[L].next,0,sizeof(tire[L].next)); tire[L].fail=NULL; tire[L].cnt=0; return &tire[L++]; } void ini(){ L=head=tail=0; root=createnode(); } void Insert(char str[]){ node *cur=root; for(int i=0;str[i];i++){ int val=str[i]-'a'; if(!cur->next[val]){ cur->next[val]=createnode(); } cur=cur->next[val]; } cur->cnt++; } void build(){ que[head++]=root; while(tail<head){ node *cur=que[tail++]; for(int i=0;i<26;i++){ if(cur->next[i]!=NULL){ node *tmp=cur; tmp=tmp->fail; while(tmp!=NULL){ if(tmp->next[i]!=NULL){ cur->next[i]->fail=tmp->next[i]; break; } tmp=tmp->fail; } if(tmp==NULL) cur->next[i]->fail=root; que[head++]=cur->next[i]; } } } } int query(char str[]){ int ans=0; node *cur=root; for(int i=0;str[i];i++){ int val=str[i]-'a'; while(cur!=root&&cur->next[val]==NULL) cur=cur->fail; cur= cur->next[val]; cur=(cur==NULL) ? root:cur; node *tmp = cur; while(tmp!=root){ ans+=tmp->cnt; tmp->cnt=0; tmp=tmp->fail; } } return ans; } }ac; int main(){ int T; scanf("%d",&T); while(T--){ ac.ini(); int m; scanf("%d",&m); while(m--){ scanf("%s",s); ac.Insert(s); } ac.build(); scanf("%s",desc); printf("%d ",ac.query(desc)); } return 0; }

tire圖優(yōu)化模版:(可見(jiàn)效力提高了很多)

//249MS 28308K #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; #define MAXN 500500 #define M 1001000 char s[55]; char desc[M]; struct node{ node *next[26]; node *fail; int cnt; }trie[MAXN],*que[MAXN],*root; struct AC{ int head,tail,L; node* createnode(){ trie[L].fail=NULL; memset(trie[L].next,0,sizeof(trie[L].next)); trie[L].cnt=0; return &trie[L++]; } void ini(){ head=tail=L=0; root=createnode(); } void Insert(char str[]){ node *cur=root; for(int i=0;str[i];i++){ int val=str[i]-'a'; if(cur->next[val]==NULL) cur->next[val]=createnode(); cur=cur->next[val]; } cur->cnt++; } void build(){ que[head++]=root; while(head>tail){ node *cur=que[tail++]; for(int i=0;i<26;i++){ if(cur->next[i]!=NULL){ if(cur==root) cur->next[i]->fail=root; else cur->next[i]->fail=cur->fail->next[i]; que[head++]=cur->next[i]; } else { if(cur==root) cur->next[i]=root; else cur->next[i] = cur->fail->next[i]; } } } } int query(char str[]){ int ans=0; node *cur=root; for(int i=0;str[i];i++){ int val=str[i]-'a'; cur=cur->next[val]; if(cur->cnt){ node *tmp=cur; while(tmp!=root){ ans+=tmp->cnt; tmp->cnt=0; tmp=tmp->fail; } } } return ans; } }ac; int main(){ int T; scanf("%d",&T); while(T--){ ac.ini(); int m; scanf("%d",&m); while(m--){ scanf("%s",s); ac.Insert(s); } ac.build(); scanf("%s",desc); printf("%d ",ac.query(desc)); } return 0; }



生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 婷婷在线成人免费观看搜索 | 国产极品久久 | 欧美人与牲禽ⅹxxx伦交 | 波多野结衣三区 | 久久xxx| 白浆都出来了视频国产精品 | 午夜欧美福利 | 欧美又粗又硬又黄又爽视频 | 精品国产91久久久久 | 久久成人免费视频 | 欧美一二三区视频 | 久久a级片 | 成人网在线看 | 亚洲三级精品 | 精品伊人网| 国产在线高清不卡免费播放 | 2020久久精品永久免费 | www一区二区| 国内精品免费视频精选在线观看 | 国产高清一区二区三区 | 在线观看 日韩 | 亚洲视频第二页 | 久久久亚洲精品视频 | 欧美日韩一区二区三区自拍 | 亚洲精品不卡视频 | 国产高清在线精品一区在线 | 亚洲成年网站在线777 | 久久精品九九亚洲精品天堂 | 一级成人a做片免费 | 日本一区二区三区视频在线观看 | 一区二区成人国产精品 | 欧美又大粗又爽又黄大片视频 | 日韩成人免费aa在线看 | 日韩特级片 | 精品一区精品二区 | 中文字幕 日本 | 日本欧美一区二区三区免费不卡 | 性加拿大高清xxxxx | 性欧美暴力猛交69hd | 国产精品不卡片视频免费观看 | 丁香综合五月 |