HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本。
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.
InputFirst 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.
OutputPrint how many keywords are contained in the description.Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
第一版:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
int Next[maxn][],End[maxn],fail[maxn];
int cnt,root=,ans;//root不能为0
void _init()
{
memset(Next,,sizeof(Next));
memset(End,,sizeof(End));
memset(fail,,sizeof(fail));
ans=;cnt=;
}
void _insert(char s[])
{
int L=strlen(s);
int now=root;
for(int i=;i<L;i++){
if(!Next[now][s[i]-'a']) Next[now][s[i]-'a']=++cnt;
now=Next[now][s[i]-'a'];
}
End[now]++;
}
void _build()//bfs
{
queue<int>q;
q.push(root);
while(!q.empty()){
int Now=q.front();q.pop();
for(int i=;i<;i++){
if(Next[Now][i]){
if(Now==root) fail[Next[Now][i]]=root;
else{
int p=fail[Now];
while(p){//给儿子们找对象
if(Next[p][i]){
fail[Next[Now][i]]=Next[p][i];
break;//找到了就停止
}
p=fail[p];
}
if(!p) fail[Next[Now][i]]=root;//不晓得?
}
q.push(Next[Now][i]);
}
}
}
}
void _query(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=;i<L;i++){
int x=s[i]-'a';
while(Now!=root&&!Next[Now][x]) Now=fail[Now];
Now=Next[Now][x];
if(!Now) Now=root;//不晓得?
int tmp=Now;
while(tmp!=root){
if(End[tmp]>=){
ans+=End[tmp];
End[tmp]=-;//避免重复
}
else break;
tmp=fail[tmp];
}
}
}
int main()
{
char s[];
char c[maxn] ;
int n,j,i,T;
scanf("%d",&T);
while(T--){ _init();
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
scanf("%s",c); _build();
_query(c);//文章 printf("%d\n",ans); }
return ;
}
稍微改进版:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
int Next[maxn][],End[maxn],fail[maxn];
int cnt,root=,ans;
int q[],tail,head;
void _init()
{
//不要memset只要200ms
ans=;cnt=;fail[root]=-;//root的fail不能等于本身,不然不能重新开始。
head=tail=;End[root]=;
for(int i=;i<;i++) Next[root][i]=;
}
void _insert(char s[])
{
int now=root;
for(int i=;s[i];i++){
if(!Next[now][s[i]-'a']) {
Next[now][s[i]-'a']=++cnt;
fail[cnt]=;
End[cnt]=;
for(int i=;i<;i++) Next[cnt][i]=;
}
now=Next[now][s[i]-'a'];
}
End[now]++;
}
void _build()//bfs
{
q[++head]=root;
while(tail<head){
int Now=q[++tail];
for(int i=;i<;i++){
if(Next[Now][i]){
if(Now==root) fail[Next[Now][i]]=root;
else{
int p=fail[Now];
while(p!=-){//给儿子们找对象
if(Next[p][i]){
fail[Next[Now][i]]=Next[p][i];
break;//找到了就停止
}
p=fail[p];//配对
}
if(p==-) {
fail[Next[Now][i]]=root;//重新开始
}
}
q[++head]=Next[Now][i];
}
}
}
}
void _query(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=;i<L;i++){
int x=s[i]-'a';
while(Now!=root&&!Next[Now][x]) Now=fail[Now];
Now=Next[Now][x];
if(Now==-) Now=root;//即使失败,也不气馁,一切从零开始
int tmp=Now;
while(tmp!=root){
if(End[tmp]>){
ans+=End[tmp];
End[tmp]=-;//避免重复
}
else break;
tmp=fail[tmp];
}
}
}
int main()
{
char s[];
char c[maxn] ;
int n,j,i,T;
scanf("%d",&T);
while(T--){ _init();
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
scanf("%s",c); _build();
_query(c);//文章 printf("%d\n",ans); }
return ;
}
HDU2222 Keywords Search ac自动机第一题的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- 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自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...
随机推荐
- SQL优化- in和not in
in不会导致索引失效,但最终数据库会将in语句解析为or语句,eg: select * from T_MAIN_PROCESS t where t.audit_status_code in ('05' ...
- Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯
1. 原理图 2. 示例代码 (网络图片查看器) (1) HttpURLConnection (2) SmartImageView (开源框架:https://github.com/loopj/an ...
- Android安装过程出现问题
Android安装过程出现问题 一.Eclipse 中 Emulator Control 不能用问题 在官方文档中发现问题所在(官方文档说明),在最后一行“The Emulator Control t ...
- python类常用装饰器
class Myclass(object): def __init__(self): pass #必须实例化才能调用 def sayhi(self): print 'hello' #静态方法,跟类没什 ...
- Qt5全局热键-QxtGlobalShortcut
最近做一个项目需要注册全局热键,在网上搜索发现有个第三方库 libqxt 中给出一个比较好的跨平台的解决方案,就是 QxtGlobalShortcut. 但是编译过程中发现这个库用到的QAbstrac ...
- day7-python类反射
一.概述 一般的高阶语言都有反射的功能特性,python也不例外,网上资料显示,python支持类反射和模块反射,今天就先学习一下类反射的相关知识,模块反射后续再展开把.Python的类反射用于把字符 ...
- js中的执行环境及作用域
最近在面试时被问到了对作用域链的理解,感觉当时回答的不是很好,今天就来说说js中的作用域链吧. 首先来说说js中的执行环境,所谓执行环境(有时也称环境)它是JavaScript中最为重要的一个概念.执 ...
- iOS自动化探索(九)使用Jenkins自动化打包并发布iOS App
继前一篇: Mac环境下安装Jenkins Jenkins安装好后, 我们试着创建一个iOS自动打包并发布的任务 iOS App构建必须在MAC上面使用xcode进行,所以我们要安装下xcode集成插 ...
- 练习-99乘法表 token生成器 翻译小工具
一.99乘法表 1.1 技术点 记住: for 循环的使用,以及for的嵌套使用 range()的使用,掌握sep为负数的使用的使用. print() 函数的使用,默认的结尾的换行符 替换 end= ...
- [C#]委托实例分析(附源码)
一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...