题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A

Description

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. 
Wiskey also wants to bring this feature to his image retrieval system. 
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched. 
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match. 
 

Input

First line will contain one integer means how many cases will follow by. 
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000) 
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50. 
The last line is the description, and the length will be not longer than 1000000. 
 

Output

Print how many keywords are contained in the description.
 

Sample Input

1
5
she
he
say
shr
her
yasherhs
 

Sample Output

3
 
题意:给了n个模式串,然后给了一个长串,求这个长串的子串中出现了多少个模式串。
 
思路:使用AC自动机的多模式匹配算法,建立trie树,然后使用bfs搜索求所有串中字母的fail指针,然后利用trie树进行模式匹配,求解。
 
代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 500010
char str[],keyword[];
int head,tail; struct node
{
node *fail;
node *next[];
int count;
node()
{
fail=NULL;
count=;
for(int i=;i<;i++)
next[i]=NULL;
}
}*q[N];
node *root; void insert(char *str) ///建立Trie
{
int temp,len;
node *p=root;
len=strlen(str);
for(int i=;i<len;i++)
{
temp=str[i]-'a';
if(p->next[temp]==NULL)
p->next[temp]=new node();
p=p->next[temp];
}
p->count++;
} void setfail() ///使用bfs初始化fail指针;
{
q[tail++]=root;
while(head!=tail)
{
node *p=q[head++];
node *temp=NULL;
for(int i=;i<;i++)
if(p->next[i]!=NULL)
{
if(p==root) ///首字母的fail必指向根
p->next[i]->fail=root;
else
{
temp=p->fail; ///失败指针
while(temp!=NULL) ///2种情况结束:匹配为空or找到匹配
{
if(temp->next[i]!=NULL) ///找到匹配
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) ///为空则从头匹配
p->next[i]->fail=root;
}
q[tail++]=p->next[i]; ///入队
}
}
} int query()
{
int index,len,result;
node *p=root;
result=;
len=strlen(str);
for(int i=;i<len;i++)
{
index=str[i]- 'a';
while(p->next[index]==NULL&&p!=root) ///跳转失败指针
p=p->fail;
p=p->next[index];
if(p==NULL)
p=root;
node *temp=p; ///p不动,temp计算后缀串
while(temp!=root&&temp->count!=-)
{
result+=temp->count;
temp->count=-;
temp=temp->fail;
}
}
return result;
} int main()
{
int T, num;
scanf("%d",&T);
while(T--)
{
head=tail=;
root = new node();
scanf("%d", &num);
getchar();
for(int i=;i<num;i++)
{
gets(keyword);
insert(keyword);
}
setfail();
scanf("%s",str);
printf("%d\n",query());
}
return ;
}
 

AC自动机---Keywords Search的更多相关文章

  1. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  2. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  3. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  4. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. hdu----(2222)Keywords Search(ac自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  8. HDU2222 Keywords Search(AC自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. hdu2222 Keywords Search【AC自动机】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. josephus Problem 中级(使用数组模拟链表,提升效率)

    问题描写叙述: 在<josephus Problem 0基础(使用数组)>中.我们提出了一种最简单直接的解决方式. 可是,细致审视代码之后.发现此种方案的效率并不高,详细体如今.当有人出局 ...

  2. Openvswitch原理与代码分析(6):用户态流表flow table的操作

    当内核无法查找到流表项的时候,则会通过upcall来调用用户态ovs-vswtichd中的flow table. 会调用ofproto-dpif-upcall.c中的udpif_upcall_hand ...

  3. winform c#绑定combobox下拉框 年度代码。

    winform c#绑定combobox下拉框 年度代码. comboBox1.Items.AddRange("});//邦定数据 comboBox1.Text = DateTime.Now ...

  4. [原创]HierarchyView的实现原理和Android设备无法使用HierarchyView的解决方法

    最近在看一个老外写的东西,发现里面有个类,使用这个类可以让任何设备使用HierarchyView. 众所周知,市面上卖的Android设备,一般都不能使用HierarchyView,所以借此机会,了解 ...

  5. C# WinForm RDLC报表不预览直接连续打印

    用微软的RDLC报表直接打印不预览 直接上代码. //打印清单 System.Data.DataTable dt = print_QD(dr); ReportViewer rvDoc = new Re ...

  6. [转]c++流缓冲---rdbuf()

    C++标准库封装了一个缓冲区类streambuf,以供输入输出流对象使用.每个标准C++输出输出流对象都包含一个指向streambuf的指针,用 户可以通过调用rdbuf()成员函数获得该指针,从而直 ...

  7. ODAC(V9.5.15) 学习笔记(十九)主键值自动生成

    ODAC支持通过Oracle的序列来自动生成表的主键功能.这个过程允许在客户端自动完成,不需要过多代码.这个对一些要求自动增长字段做主键的场合非常有用.其实现步骤为: 1.数据库必须先建立生成主键的序 ...

  8. IIS7 php wordpress 中文url 标签tag中文URL404解决方法

    新建重写规则: <rule name="ChineseURL" stopProcessing="true"> <match url=" ...

  9. 转:php park、unpark、ord 函数使用方法(二进制流接口应用实例)

    在工作中,我也逐渐了解到park,unpark,ord对于二进制字节处理的强大. 下面我逐一介绍它们.     park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多. 我在最近一 ...

  10. eclipse从数据库逆向生成Hibernate实体类

    做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...