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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU-3065 病毒侵袭持续中 AC自动机又是一板子!

發布時間:2023/12/2 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU-3065 病毒侵袭持续中 AC自动机又是一板子! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

病毒侵襲持續中

? ?上一題是求出現多少病毒輸出病毒序號,而這題輸出每個病毒出現的次數。這題有字典樹基礎都能做出來,把葉子節點用相應的編號標記起來,匹配的時候遍歷到葉子節點用一個數組把次數存起來就行了。

? ? 有了前幾題的教訓直接用靜態樹做用C++交,果然不會錯!

const int N=129; char str[2000001],s[1001][55],v[1001]; int head,tail,cnt; struct tree {int f;tree *fail;tree *next[N]; }*q[50001],*root,memory[50001]; void insert(int i,char *s) {tree *p=root;while(*s!='\0'){int id=(int)(*s);if(p->next[id]==NULL) p->next[id]=&memory[cnt++];p=p->next[id];s++;}p->f=i; } void build() {root->fail=NULL;q[head++]=root;while(head!=tail){tree *temp=q[tail++];tree *p=NULL;for(int i=0; i<N; i++)if(temp->next[i]){if(temp==root) temp->next[i]->fail=root;else{p=temp->fail;while(p!=NULL){if(p->next[i]){temp->next[i]->fail=p->next[i];break;}p=p->fail;}if(p==NULL) temp->next[i]->fail=root;}q[head++]=temp->next[i];}} } void find(char *s) {tree *p=root;int ans=0;while(*s!='\0'){int id=(int)(*s);while(p->next[id]==NULL&&p!=root) p=p->fail;p=p->next[id];p=p==NULL?root:p;tree *temp=p;while(temp!=root){if(temp->f) v[temp->f]++;temp=temp->fail;}s++;} } int main() {int n;while(~scanf("%d",&n)){memset(memory,0,sizeof(memory));memset(v,0,sizeof(v));cnt=head=tail=0;root=&memory[cnt++];for(int i=1; i<=n; i++){scanf("%s",s[i]);insert(i,s[i]);}build();scanf("%s",str);find(str);for(int i=1; i<=n; i++)if(v[i]) printf("%s: %d\n",s[i],v[i]);}return 0; }



轉載于:https://www.cnblogs.com/nyist-TC-LYQ/p/7208123.html

總結

以上是生活随笔為你收集整理的HDU-3065 病毒侵袭持续中 AC自动机又是一板子!的全部內容,希望文章能夠幫你解決所遇到的問題。

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