hdu 2896 病毒侵袭 (ac自动机)
来源:互联网 发布:淘宝网实拍保护入口 编辑:程序博客网 时间:2024/05/21 10:10
思路分析:
query 的时候不要把count 置为 0
坑就是他不只有字母= =
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int max_next = 130;struct trie{ struct trie *fail; struct trie *next[max_next]; int count;};trie *Q[500005];int head,tail;char str[1000005];void insert(trie *root,char *word,int id){ trie *p=root; int i=0; while(word[i]!='\0') { if(p->next[word[i]-' ']==NULL) { trie *temp = new trie; for(int j=0;j<max_next;j++)temp->next[j]=NULL; temp->count=0; temp->fail=NULL; p->next[word[i]-' ']=temp; } p=p->next[word[i]-' ']; i++; } p->count=id;}void acbuild(trie *root){ root->fail=NULL; head=tail=0; Q[tail++]=root; while(head<tail) { trie *temp=Q[head++]; trie *p=NULL; for(int i=0;i<max_next;i++) { if(temp->next[i]!=NULL) { if(temp==root)temp->next[i]->fail=root; else { p=temp->fail; while(p!=NULL) { if(p->next[i]!=NULL) { temp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL)temp->next[i]->fail=root; } Q[tail++]=temp->next[i]; } } }}int ans[1111111];int top;bool vis[111111];bool query(trie *root){ int i=0; bool cnt=0; int len=strlen(str); trie *p=root; int index; while(str[i]) { int index = str[i]-' '; while(p->next[index]==NULL && p!=root) p=p->fail; p=p->next[index]; if(p==NULL)p=root; trie *temp=p; while(temp!=NULL) { if(temp->count)ans[top++]=temp->count; cnt=true; //cnt+=temp->count; //temp->count=0; temp=temp->fail; } i++; } return cnt;}void del(trie *root){ for(int i=0;i<max_next;i++) if(root->next[i])del(root->next[i]); free(root);}char word[555];int main(){ int n; while(scanf("%d",&n)!=EOF) { trie *root = new trie; for(int i=0;i<max_next;i++)root->next[i]=NULL; root->count=0; root->fail=NULL; for(int i=1;i<=n;i++) { scanf("%s",word); insert(root,word,i); } acbuild(root); int m; scanf("%d",&m); int fans=0; for(int cas=1;cas<=m;cas++) { top=0; scanf("%s",str); query(root); fans+=(top==0?0:1); if(top){ sort(ans,ans+top); top=unique(ans,ans+top)-ans; printf("web %d:",cas); for(int i=0;i<top;i++)printf(" %d",ans[i]); puts(""); } } printf("total: %d\n",fans); //del(root); } return 0;}/*3aaabbbccc2bbbaaccaaabbbcccbbb*/
0 0
- HDU 2896 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭 //AC自动机
- HDU 2896:病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 AC自动机
- HDU 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 AC自动机基础
- HDU 2896 病毒侵袭 AC自动机
- hdu 2896 病毒侵袭 -- AC自动机
- hdu 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭(AC 自动机)
- HDU 2896 病毒侵袭 (AC自动机)
- HDU 2896 病毒侵袭 【AC自动机】
- hdu 2896 病毒侵袭 AC自动机
- [HDU 2896]病毒侵袭[AC自动机]
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- hdu 2896 病毒侵袭 (AC自动机)
- hdu 2896 病毒侵袭(AC自动机)
- CM9源码下载和编译
- Swift中文教程(四) 集合类型
- TortoiseSVN无法查看日志
- datatables 自定义布局
- linux下创建用户
- hdu 2896 病毒侵袭 (ac自动机)
- Solr使用入门指南
- abs, fabs, fabsf 区别
- Git使用
- ViewPager中的EditText设置监听器就会出错的解决办法
- webclient在访问期间出现异常,报错! 无法发送具有此谓词类型的内容正文
- PuTTY使用密钥登录到Linux
- Params可变参数
- solr学习有关