病毒侵袭(我的第二道AC自动机(模板进一步升级完善))

来源:互联网 发布:澳洲大学申请时间 知乎 编辑:程序博客网 时间:2024/04/29 02:05


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

病毒侵袭

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15750    Accepted Submission(s): 4017


Problem Description
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
 

Input
第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
 

Output
依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
 

Sample Input
3aaabbbccc2aaabbbcccbbaacc
 

Sample Output
web 1: 1 2 3total: 1
 

Source
2009 Multi-University Training Contest 10 - Host by NIT
 


AC code:
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;struct Trie{int next[210*500][128];//有128个可显字符(若题目给的只有小写字母,128可能会暴),还有,注意第一个下标容量大小!!!//防止MLE或RE(access_violation),下面开的数组大小类似上面的数组第一个下标容量大小,//第一个下标表示字典树最大结点数,也就是允许的最大总的字符总数 int fail[210*500];//失配指针  int end[210*500];//记录数组,若要输出出现模式串的id,用end记录idint count[210*500];//计数器 int root;//根结点指针 int L;//总长度int NewNode() //获取新结点并初始化  {for(int i=0;i<128;i++){next[L][i]=-1;}end[L]=-1;count[L]=0;return L++;}void init() //初始化{L=0;root=NewNode();}void Insert(char *s,int id){int len=strlen(s);int j=root;for(int i=0;i<len;i++){if(next[j][s[i]]==-1)//不存在该结点{next[j][s[i]]=NewNode();}j=next[j][s[i]];}end[j]=id;//记录其id  count[j]++;}void build(){queue<int>q;fail[root]=root;//根结点失配指针指向自己  //根结点的孩子入队,其失配指针指向自己for(int i=0;i<128;i++){if(next[root][i]==-1)//不存在该孩子 {next[root][i]=root;//指向自己}else{fail[next[root][i]]=root;//失配指针指向自己  q.push(next[root][i]); //孩子入队  }}int j;while(!q.empty()){j=q.front();q.pop();for(int i=0;i<128;i++){if(next[j][i]==-1)//不存在该孩子,指向其父结点失配指针所指向的结点(该结点也有孩子i)  {next[j][i]=next[fail[j]][i];}else{fail[next[j][i]]=next[fail[j]][i];q.push(next[j][i]);}}}}bool used[510];//记录数组  int query(char *str,int n,int id){int len=strlen(str);int j=root;int temp;int ans1=0;//how many keywords will be match,即求目标串中出现了几个模式串(同一个模式串允许在源串的不同位置重复出现,出现一次,次数累加一次) int ans2=0;//出现的模式串个数(同一个模式串不允许在源串的不同位置重复出现,即使出现多次,次数只算一次)  memset(used,false,sizeof(used));for(int i=0;i<len;i++){j=next[j][str[i]];temp=j;while(temp!=root){ans1+=count[temp];count[temp]=0;if(end[temp]!=-1) //该单词或字符在Trie中出现了 {used[end[temp]]=true;//记录ans2++;}temp=fail[temp];//继续找后缀串}}if(ans2>0)//按字典序输出已出现过的模式串 {printf("web %d:",id);for(int i=1;i<=n;i++){if(used[i])printf(" %d",i);}printf("\n");}//return ans1;//返回目标串中出现的模式串个数(出现多次,次数累加算) return ans2;//返回目标串中已出现过的模式串个数(出现多次,次数只算一次)}};char str[MAXN];Trie ac;int main(){    int n,m,tot,t;    while(scanf("%d",&n)!=EOF)    {    ac.init();    for(int i=1;i<=n;i++)    {    scanf("%s",str);    ac.Insert(str,i);}ac.build();tot=0;scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%s",str);if(ac.query(str,n,i)>0)tot++;}printf("total: %d\n",tot);}    return 0;}



改进简化代码(省去了ans2变量):
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;struct Trie{int next[210*500][128];//有128个可显字符(若题目给的只有小写字母,128可能会暴),还有,注意第一个下标容量大小!!!//防止MLE或RE(access_violation),下面开的数组大小类似上面的数组第一个下标容量大小,//第一个下标表示字典树最大结点数,也就是允许的最大总的字符总数 int fail[210*500];//失配指针  int end[210*500];//记录数组,若要输出出现模式串的id,用end记录idint count[210*500];//计数器 int root;//根结点指针 int L;//总长度int NewNode() //获取新结点并初始化  {for(int i=0;i<128;i++){next[L][i]=-1;}end[L]=-1;count[L]=0;return L++;}void init() //初始化{L=0;root=NewNode();}void Insert(char *s,int id){int len=strlen(s);int j=root;for(int i=0;i<len;i++){if(next[j][s[i]]==-1)//不存在该结点{next[j][s[i]]=NewNode();}j=next[j][s[i]];}end[j]=id;//记录其id  count[j]++;}void build(){queue<int>q;fail[root]=root;//根结点失配指针指向自己  //根结点的孩子入队,其失配指针指向自己for(int i=0;i<128;i++){if(next[root][i]==-1)//不存在该孩子 {next[root][i]=root;//指向自己}else{fail[next[root][i]]=root;//失配指针指向自己  q.push(next[root][i]); //孩子入队  }}int j;while(!q.empty()){j=q.front();q.pop();for(int i=0;i<128;i++){if(next[j][i]==-1)//不存在该孩子,指向其父结点失配指针所指向的结点(该结点也有孩子i)  {next[j][i]=next[fail[j]][i];}else{fail[next[j][i]]=next[fail[j]][i];q.push(next[j][i]);}}}}bool used[510];//记录数组  int query(char *str,int n,int id){int len=strlen(str);int j=root;int temp;int ans1=0;//how many keywords will be match,即求目标串中出现了几个模式串(同一个模式串允许在源串的不同位置重复出现,出现一次,次数累加一次)   memset(used,false,sizeof(used));for(int i=0;i<len;i++){j=next[j][str[i]];temp=j;while(temp!=root){                                ans1+=count[temp];//count[temp]=0;//这里要注释掉才能过,不然WA,因为不注释掉的话在第一个源串匹配成功后字典树上该节点的值自动置为0,下一个源串匹配时就匹配不到了,一般来说,有多源串同时匹配多模式串时要注释掉,除非是求所有模式串中有几个在源串出现过,则不能注释掉。这种情况下在下一个源串匹配时只能再重新初始化trie  init和建树build,或者也可以新增一个ac自动机的变量ac2作为副本,把第一次建好的ac赋给ac2就可以了。if(end[temp]!=-1) //该单词或字符在Trie中出现了 {used[end[temp]]=true;//记录}temp=fail[temp];//继续找后缀串}}if(ans1>0)//按字典序输出已出现过的模式串 {printf("web %d:",id);for(int i=1;i<=n;i++){if(used[i])printf(" %d",i);}printf("\n");}return ans1;//返回目标串中出现的模式串个数(出现多次,次数累加算) }};char str[MAXN];Trie ac;//有需要的话在这里再添加一个ac2变量作为副本int main(){    int n,m,tot,t;    while(scanf("%d",&n)!=EOF)    {    ac.init();    for(int i=1;i<=n;i++)    {    scanf("%s",str);    ac.Insert(str,i);}ac.build();tot=0;scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%s",str);if(ac.query(str,n,i)>0)tot++;}printf("total: %d\n",tot);}    return 0;}


0 0
原创粉丝点击