杭电2896(HDU2896)
来源:互联网 发布:sql语言怎么查询数据 编辑:程序博客网 时间:2024/06/03 21:45
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100010
struct node
{
node *fail;
node *next[94];
int count,id;
node()
{
fail=NULL;
count=0;
for(int i=0;i<94;i++)
next[i]=NULL;
}
}*q[N*6];
node *root;
char s[N],str[220];
int vis[550];//标记数组,用来判断该字符串是否出现
void insert(char *s,int c)
{
int i,j,len=strlen(s);
node *p=root;
for(i=0;i<len;i++)
{
j=s[i]-32;
if(p->next[j]==NULL)
p->next[j]=new node();
p=p->next[j];
}
p->count++;
p->id=c+1;
}
void build()
{
int i,j,head=0,tail=0;
node *p,*tmp;
q[tail++]=root;
while(head!=tail)
{
p=q[head++];
for(i=0;i<94;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)
p->next[i]->fail=root;
else
{
tmp=p->fail;
while(tmp!=NULL)
{
if(tmp->next[i]!=NULL)
{
p->next[i]->fail=tmp->next[i];
break;
}
tmp=tmp->fail;
}
if(tmp==NULL)
p->next[i]->fail=root;
}
q[tail++]=p->next[i];
}
}
}
}
int query(char *s)
{
int i,j,len=strlen(s),flag=0;
node *p=root,*tmp;
memset(vis,0,sizeof(vis));
for(i=0;i<len;i++)
{
j=s[i]-32;
while(p->next[j]==NULL&&p!=root)
p=p->fail;
p=p->next[j];
if(p==NULL)
p=root;
tmp=p;
while(tmp!=NULL)
{
if(tmp->count>0)
{
flag=1;
vis[tmp->id]=1;
}
tmp=tmp->fail;
}
}
return flag;
}
int main()
{
int n,m,i,j;
while(scanf("%d",&n)!=EOF)
{
root=new node();
for(i=0;i<n;i++)
{
scanf("%s",str);
insert(str,i);
}
build();
scanf("%d",&m);
int ans=0;
for(i=0;i<m;i++)
{
scanf("%s",s);
if(query(s))
{
ans++;
printf("web %d:",i+1);
for(j=1;j<=n;j++)
if(vis[j])
printf(" %d",j);
printf("\n");
}
}
printf("total: %d\n",ans);
}
return 0;
}
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100010
struct node
{
node *fail;
node *next[94];
int count,id;
node()
{
fail=NULL;
count=0;
for(int i=0;i<94;i++)
next[i]=NULL;
}
}*q[N*6];
node *root;
char s[N],str[220];
int vis[550];//标记数组,用来判断该字符串是否出现
void insert(char *s,int c)
{
int i,j,len=strlen(s);
node *p=root;
for(i=0;i<len;i++)
{
j=s[i]-32;
if(p->next[j]==NULL)
p->next[j]=new node();
p=p->next[j];
}
p->count++;
p->id=c+1;
}
void build()
{
int i,j,head=0,tail=0;
node *p,*tmp;
q[tail++]=root;
while(head!=tail)
{
p=q[head++];
for(i=0;i<94;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)
p->next[i]->fail=root;
else
{
tmp=p->fail;
while(tmp!=NULL)
{
if(tmp->next[i]!=NULL)
{
p->next[i]->fail=tmp->next[i];
break;
}
tmp=tmp->fail;
}
if(tmp==NULL)
p->next[i]->fail=root;
}
q[tail++]=p->next[i];
}
}
}
}
int query(char *s)
{
int i,j,len=strlen(s),flag=0;
node *p=root,*tmp;
memset(vis,0,sizeof(vis));
for(i=0;i<len;i++)
{
j=s[i]-32;
while(p->next[j]==NULL&&p!=root)
p=p->fail;
p=p->next[j];
if(p==NULL)
p=root;
tmp=p;
while(tmp!=NULL)
{
if(tmp->count>0)
{
flag=1;
vis[tmp->id]=1;
}
tmp=tmp->fail;
}
}
return flag;
}
int main()
{
int n,m,i,j;
while(scanf("%d",&n)!=EOF)
{
root=new node();
for(i=0;i<n;i++)
{
scanf("%s",str);
insert(str,i);
}
build();
scanf("%d",&m);
int ans=0;
for(i=0;i<m;i++)
{
scanf("%s",s);
if(query(s))
{
ans++;
printf("web %d:",i+1);
for(j=1;j<=n;j++)
if(vis[j])
printf(" %d",j);
printf("\n");
}
}
printf("total: %d\n",ans);
}
return 0;
}
阅读全文
0 0
- 杭电2896(HDU2896)
- hdu2896
- hdu2896
- HDU2896
- hdu2896
- HDU2896
- hdu2896 病毒侵袭
- hdu2896 病毒侵袭
- hdu2896 AC自动机
- hdu2896 AC自动机
- HDU2896--病毒侵袭
- hdu2896之AC自动机
- hdu2896(ac自动机)
- hdu2896 AC自动机
- hdu2896 ac自动机
- hdu2896 & hdu3065 ac自动机
- hdu2896 AC自动机
- hdu2896-ac自动机
- Uva 340 猜数字的游戏
- oracle 数据字典
- 利用tensorflow 一步一步实现一个简单神经网络,线性回归
- 2017多校第二场 HDU 6050 Funny Function 推公式
- 1084. Broken Keyboard (20)
- 杭电2896(HDU2896)
- html 简单注册页面
- 抽象类Abstract 和接口 Interface
- 关于使用枚举
- DDoS攻击类型
- git配置与使用
- C语言基础之流程控制
- 在html中常用哪几种方式定义css
- 动态添加线程,然后监控多个线程是否都完成了