hdu 2222 ac自动机模版
来源:互联网 发布:万圣节面具淘宝 编辑:程序博客网 时间:2024/05/22 08:26
#include<stdio.h>
#include<string.h>
struct
node
{
int
num;
node *next[26],*fail;
node()
{
num=0;
fail=NULL;
for
(
int
i=0;i<26;i++)
next[i]=NULL;
}
}*q[1000010];
char
ss[1010],s[55];
void
insert(
char
s[],node *root)
{
int
len=
strlen
(s);
node *p=root;
for
(
int
i=0;i<len;i++)
{
if
(p->next[s[i]-
'a'
]==NULL)
p->next[s[i]-
'a'
]=
new
node();
p=p->next[s[i]-
'a'
];
}
p->num++;
}
void
acmove(node *root)
{
int
head=0,tail=1;
root->fail=NULL;
q[0]=root;
while
(head!=tail)
{
node *now=q[head++];
node *p=NULL;
for
(
int
i=0;i<26;i++)
{
if
(now->next[i]!=NULL)
{
if
(now==root) now->next[i]->fail=root;
else
{
p=now->fail;
while
(p!=NULL)
{
if
(p->next[i]!=NULL)
{
now->next[i]->fail=p->next[i];
break
;
}
p=p->fail;
}
if
(p==NULL)
now->next[i]->fail=root;
}
q[tail++]=now->next[i];
}
}
}
}
int
find(node *root)
{
int
i=0,numb=0,len=
strlen
(ss);
node *p=root;
while
(ss[i])
{
while
(p->next[ss[i]-
'a'
]==NULL&&p!=root)
p=p->fail;
p=p->next[ss[i]-
'a'
];
p=(p==NULL)?root:p;
node *now=p;
while
(now!=root&&now->num!=0)
{
numb+=now->num;
now->num=0;
now=now->fail;
}
i++;
}
return
numb;
}
int
main()
{
int
n;
node *root;
scanf
(
"%d"
,&n);
while
(n--)
{
int
N;
root=NULL;
root=
new
node();
scanf
(
"%d"
,&N);
for
(
int
i=0;i<N;i++)
{
scanf
(
"%s"
,s);
insert(s,root);
}
acmove(root);
scanf
(
"%s"
,ss);
int
w=find(root);
printf
(
"%dn"
,w);
}
return
0;
}
- hdu 2222 ac自动机模版
- HDU 2222 AC自动机模版
- HDU 2222 AC自动机模版题
- HDU 2222 AC自动机模版题
- HDU 5384 AC自动机模版
- ac自动机模版(hdu 5384)
- HDU 1277 HDU 2222 AC自动机模版题
- hdu 2222 Keywords Search(AC自动机模版题)
- HDU 2222 Keywords Search AC自动机入门模版题
- HDU 2222-Keywords Search-AC自动机模版题
- HDU 2222 Keywords Search (AC自动机模版题)
- HDU ACM 2222->AC自动机模版题(入门题)
- hdu 2222 Keywords Search(AC自动机入门模版题)
- hdu 2896 AC自动机模版题
- hdu 3065 AC自动机模版题
- hdu 2896 病毒侵袭 (AC自动机模版)
- HDU 2896 AC自动机模版题
- HDU 3065 AC自动机模版题
- TMS320C55x的寄存器
- 添加sudo用户
- kmp超级版
- SQL优化--处理上百万条的数据库如何提高处理查询速度
- 稳定压倒的一切:创新
- hdu 2222 ac自动机模版
- 原作者未知 Flash Webgame 开发架构与心得
- dalvikvm
- 学习MongoDB--(4-1):MongoDB查询(基本查询条件操作符介绍)
- QQC++笔试相关
- 关于默认构造函数的几个错误认识
- 【转载】WM获取GPS数据
- 学习MongoDB--(4-2):MongoDB查询(数组、内嵌文档和$where)
- tarjan 2-sat: party