HDU2896【AC自动机-模板】
2024-08-24 11:29:15
思路:
因为不同病毒特征码不会相同。
AC自动机,然后对于每一个输出即可。
注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE。
//#include <bits/stdc++.h>
#include<iostream>
#include<queue>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std; const int N=1e5+10; //500个串,长度为200 struct Trie{
int num;
Trie *next[128],*fail;
};
Trie q[N],*root;
int tol; Trie* Creat()
{
Trie *p;
p=&q[tol++];
p->fail=NULL;
p->num=0;
for(int i=0;i<128;i++)
p->next[i]=NULL;
return p;
} void Insert(char *str,int num)
{
int len=strlen(str),index;
Trie *p=root;
for(int i=0;i<len;i++)
{
index=str[i];
if(p->next[index]==NULL)
p->next[index]=Creat();
p=p->next[index];
}
p->num=num;
} void Build_Ac()
{
queue<Trie*>que;
que.push(root);
while(!que.empty())
{
Trie *p=que.front();que.pop();
for(int i=0;i<128;i++)
{
if(p->next[i]!=NULL)
{
if(p==root) p->next[i]->fail=root;
else{
Trie *temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL) {
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL)
p->next[i]->fail=root;
}
que.push(p->next[i]);
}
}
}
} int ans[510],nn;
char word[10010];
void Query()
{
Trie *p=root;
int index,len=strlen(word);
for(int i=0;i<len;i++)
{
index=word[i];
while(p!=root && p->next[index]==NULL)
p=p->fail;
p=p->next[index];
if(p==NULL)
p=root;
Trie *temp=p;
while(temp!=root)
{
if(temp->num)
ans[nn++]=temp->num;
temp=temp->fail;
}
}
} char s[220];
int main()
{
tol=0;
root=Creat();
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
Insert(s,i);
}
Build_Ac();
scanf("%d",&m);
int sum=0;
for(int i=1;i<=m;i++)
{
scanf("%s",word);
nn=0;
Query();
if(nn)
{
sum++;
sort(ans,ans+nn);
printf("web %d:",i);
for(int i=0;i<nn;i++)
printf(" %d",ans[i]);
puts("");
}
}
printf("total: %d\n",sum);
return 0;
}
最新文章
- [LeetCode] Gas Station 加油站问题
- Sublime Text 基础配置
- Mysql查看执行计划-explain
- js获取Html元素的实际宽度高度
- iOS开发网络篇—简单介绍ASI框架的使用
- ubuntu下安装kde Plasma
- navicat 或者workbench 无法连接127.0.0.1(61)的解决方法
- Android开发UI之ListView中的Button点击设置
- google code 上传源码
- cf B. The Fibonacci Segment
- android面试题 不仅仅是面试是一个很好的学习
- 创建和使用SQL Server SSAS本地多维数据集
- 从《海贼王》的视角走进BAT的世界(百度/阿里/腾讯)
- 记录一次CentOS环境升级Python2.6到Python2.7并安装最新版pip
- [SDOI2011]染色
- 使用http服务提供yum源
- Spring.Net 简单实例-01(IOC)
- Linux给普通用户增加ssh权限
- J2EE完全手册(二)
- 提交代码到自己的github仓库