AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A
Description
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
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
Sample Input
Sample Output
#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的更多相关文章
- AC日记——Keywords Search hdu 2222
		2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ... 
- 【HDU2222】Keywords Search AC自动机
		[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ... 
- hdu2222 Keywords Search ac自动机
		地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ... 
- HDU 2222 Keywords Search(AC自动机模版题)
		Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ... 
- HDU2222 Keywords Search [AC自动机模板]
		Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ... 
- hdu----(2222)Keywords Search(ac自动机)
		Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ... 
- 【HDU2222】Keywords Search(AC自动机)
		Problem Description In the modern time, Search engine came into the life of everybody like Google, B ... 
- HDU2222 Keywords Search(AC自动机)
		Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ... 
- hdu2222 Keywords Search【AC自动机】
		Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ... 
随机推荐
- Android TabActivity中onKeyDown无效问题
			@Override public boolean onKeyDown(int keyCode, KeyEvent event) { //按下键盘上返回 ... 
- [转载]堆排序(HeapSort) Java实现
			堆排序的思想是利用数据结构--堆.具体的实现细节: 1. 构建一个最大堆.对于给定的包含有n个元素的数组A[n],构建一个最大堆(最大堆的特性是,某个节点的值最多和其父节点的值一样大.这样,堆中的最大 ... 
- Eclipse+Selenium自动化测试脚本设计V1.0
			Eclipse+Selenium自动化测试脚本设计V1.0 http://www.docin.com/p-803032251.html 
- uniGUI试用笔记(十四)TUniTreeView的CheckBox
			TUniTreeView目前版本没有封装CheckBox功能,所以需要手工处理,幸好0.99版提供部分代码了,修改过程如下: 1.uniGUIAbstractClasses.pas单元中修改基类TUn ... 
- Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新
			当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ... 
- 免费国内外"代码托管服务器"收集
			国内 开源中国 http://git.oschina.net/ 支持git 淘宝code http://code.taobao.org/ 支持svn 京东code https://cod ... 
- CSS之边框覆盖
			今天想做一个淘宝导航来练练手,遇到了边框覆盖的问题.如下图: li的红色边框盖不住该灰色边框.后来问经验人士告诉我,这种边框覆盖是会出现无法100%保证正常的情况,遂得到如下3中解决方案: 1.以后遇 ... 
- 读书笔记_Effective_C++_条款四十二:了解typename的双重意义
			顾名思义,typename有双重含意.只要你用过template,那么第一重含意一定知道,那就是声明模板的时候,我们既可以这样写: template <class T> 也可以这样写 te ... 
- transition & transform
			transition: 过渡时间 被改变属性 执行函数 延迟时间 transition:width 1s,height 2s 1s; transform: 平移(translate).缩放(scale ... 
- common.css
			/* global */ document,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockqu ... 
