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 ...
随机推荐
- Run same command on all SQL Server databases without cursors
original: https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-w ...
- IOS RSA 加密方式
采用RSA加密方式,主要是生成公钥和私钥,公钥用来加密,私钥用来解密,至于其中如何实现的,网上有很多原理. 参见如下: https://github.com/jslim89/RSA-objc PS: ...
- Xcode 文档注释方法
摘自:http://www.cnblogs.com/bomo/p/4815963.html 文档注释,可以在调用时显示注释信息,让调用者更好的理解方法的用途. 注释方法: /// 注释 和 /** 注 ...
- 【python】日志模块
# -*- coding: utf-8 -*- """ Created on Thu Jul 09 09:36:59 2015 @author: dapenghuang ...
- SSAS:菜鸟摸门
官方:SSAS 多维模型 Analysis Services 多维解决方案使用多维数据集结构来分析多个维度之间的业务数据. 多维模式是 Analysis Services 的默认服务器模式. 它包括针 ...
- XCActionBar 「Xcode 中的 Alfred」
下载地址:https://github.com/pdcgomes/XCActionBar 基本命令: (1)「command+shift+8」或者双击「command」键可以打开「动作输入框窗口」 ( ...
- CSS3动画进度条
CSS3动画进度条 CSS CODE: @-webkit-keyframes move{ 0%{ background-position: 0 0; } 100%{ background-posi ...
- Linux 学习碎片
1.登录远程机器: ssh 远程机器用户名@远程机器IP ssh root@192.168.1.101 2.不同机器之前拷贝文件 #拷贝本机单个文件到远程服务器 scp /home/user1/tb. ...
- mysql安装过程中出现错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement解决
mysql安装过程中出现错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement解决 最近新装好的my ...
- Android源码下载并绑定到Eclipse中
在Windows下,通过SDK Manager.exe更新下载的Android,是不带源码的,我们开发开发起来不是很方便: 其实Android的源代码是可以下载的,其源代码入在http://andro ...