HDU 2896 病毒侵袭 (AC自动机)


题意:给你n个病毒串,m个网站,问每个网站包含有哪些有病毒的网站,输出病毒编号。

最后输出有多少个有病毒的网站。


附:AC自动机详解

Sample Input
  
  
3 aaa bbb ccc 2 aaabbbccc bbaacc
 

Sample Output
  
  
web 1: 1 2 3 total: 1
PS:web 2 没有病毒


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int kind = 94;

struct node
{
	node *fail;
	node *next[kind];
	int count;
	node()
	{
		fail = NULL;
		count = 0;
		memset(next,0,sizeof(next));
	}
}*q[1000200*2];
char keyword[220],str[10010];
int head,tail;
void insert(char *str,node *root,int num)
{
	node *p=root;
	int i=0,index;
	while(str[i])
	{
		index = str[i] - ' ';
		if(p->next[index]==NULL)
			p->next[index] = new node();
		p = p->next[index];
		i++;
	}
	p->count=num;
}

void build_ac(node *root)
{
	int i;
	root->fail=NULL;
	q[head++]=root;
	while(head!=tail)
	{
		node *temp = q[tail++];
		node *p=NULL;
		for(i=0;i<kind;i++)
		{
			if(temp->next[i]!=NULL)
			{
				if(temp==root)	
					temp->next[i]->fail=root;//失败指针指向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[head++]=temp->next[i];
			}
		}
	}
}
int vis[550];
int num[550];
int query(char *str,node *root)
{
	int i=0,cnt=0,index,len=strlen(str);
	node *p=root;
	while(str[i])
	{
		index = str[i]-' ';
		while(p->next[index]==NULL&&p!=root)
			p = p->fail;
		p=p->next[index];
		p=(p==NULL)?root:p;
		node *temp = p;
		while(temp!=root)
		{

			if(temp->count!=0)
				cnt++;
			vis[temp->count]=1;
			temp=temp->fail;
		}
		i++;
	}
	return cnt;
}

int main()
{
	int n,m;
	while(~scanf("%d",&n))
	{
		head=tail=0;
		node *root = new node();
		getchar();
		for(int i=1;i<=n;i++)
		{
			gets(keyword);
			insert(keyword,root,i);
		}
		build_ac(root);
		int total=0;
		scanf("%d",&m);
		for(int j=1;j<=m;j++)
		{
			scanf("%s",str);
			memset(vis,0,sizeof(vis));
			int k=query(str,root);
			if(k==0)
				continue;
			printf("web %d:",j);
			for(int i=1;i<=n;i++)
				if(vis[i])
					printf(" %d",i);
			printf("\n");
			total++;
		}
		printf("total: %d\n",total);

	}
	return 0;
}

/*
3
aaa
ab
cc
2
aaabcc
abbccaa

*/


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值