HDU 2896
来源:互联网 发布:税务大数据分析讲座 编辑:程序博客网 时间:2024/05/21 09:05
题意:中文题意
做法:AC自动机,注意的事项在代码中
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#define maxs 128#define MME(i,j) memset(i,j,sizeof(i))using namespace std;struct Trie{ int nexts[210*500][maxs],fail[210*500],ends[210*500]; int root,L; int newnode(){ MME(nexts[L],-1); ends[L++]=-1; return L-1; } void init(){ L=0; root=newnode(); } void Insert(char *s,int id){ int len=strlen(s),now=root; for(int i=0;i<len;i++) { if(nexts[now][s[i]] == -1)//当前节点的第i个儿子不存在 nexts[now][s[i]]=newnode(); now=nexts[now][s[i]]; } ends[now]=id; } void build(){ queue<int> q; fail[root]=root; for(int i=0;i<maxs;i++){ if(nexts[root][i] == -1)//当前根节点的 第i个儿子不存在时全部指向了根 nexts[root][i]=root; else{ fail[nexts[root][i]]=root; q.push(nexts[root][i]); } } int now; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<128;i++) if(nexts[now][i] == -1 ) nexts[now][i]=nexts[fail[now]][i]; else{ fail[nexts[now][i]]=nexts[fail[now]][i]; q.push(nexts[now][i]); } } } bool used[510]; bool query(char *s,int n,int id) { int temp,len=strlen(s),now=root; MME(used,0); bool flag=0; for(int i=0;i<len;i++){ now=nexts[now][s[i]]; temp=now; while(temp!=root){ if(ends[temp]!=-1) { used[ends[temp]]=1; flag=1; } temp=fail[temp]; } } if(flag==false) return 0; printf("web %d:",id); for(int i=1;i<=n;i++) { if(used[i]) printf(" %d",i); } puts(""); return 1; }};//AC自动机 模板,query函数需要根据题意来修改。char keyword[10010];Trie ac;int main(){ int n,m; while(~scanf("%d",&n)){ ac.init(); for(int i=1;i<=n;i++) { scanf("%s",keyword); ac.Insert(keyword,i); } ac.build(); int ans=0; scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%s",keyword); if(ac.query(keyword,n,i)) ans++; } printf("total: %d\n",ans); } return 0;}
0 0
- HDU 2896 &HDU 3065
- hdu 2896
- hdu 2896
- HDU 2896
- hdu 2896
- HDU 2896
- hdu 2896
- hdu 2896
- (2896)HDU
- hdu 2896
- hdu 2896
- HDU 2896
- hdu 2222 hdu 3065 hdu 2896 AC自动机水题
- AC自动机小结 (HDU 2222,HDU 2896,HDU 3065)
- AC自动机(hdu 2896 hdu 3065)
- hdu 2896 病毒侵袭
- hdu 2896 病毒侵袭
- hdu 2896 病毒侵袭
- Tengine(一) 之 安装Tengine
- poj1573_搜索(广搜和深搜)
- zoj 3955 矩阵+ 思维 training2
- POI操作Excel
- 专访周彦伟:十年技术老兵谈为什么MySQL最流行及DBA精神
- HDU 2896
- AsyncTask
- myeclipse新安装好之后要配置一下jdk
- ActiveMQ中消费者是如何接收消息的(二)
- 华为网络赛云平台学习知识点整理
- (二)c++类的继承与派生
- 【TensorFlow官网搬运201704】Getting Started With TensorFlow
- Linux之常用Shell命令
- 金额三位一逗显示