HDU2222 Keywords Search
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
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.
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
description.
5
she
he
say
shr
her
yasherhs
正解:AC自动机
解题报告:
AC自动机裸题。联赛前复习模板的时候突然发现AC自动机自己不会打了...好气啊
这篇博客讲的非常详细:http://blog.csdn.net/niushuai666/article/details/7002823
当然我的AC自动机还加了一条优化,就是当前结点,如果没有某个儿子结点,那么就直接连接上fail指针所指的结点的这一个儿子结点,这样的话在find的时候就不用每次都不停地往上跳fail指针了,可以直接一路顺着走,当全图都没有这个结点的时候,就会走到root上去,所以很方便,而且很快。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,cnt,tr[MAXN][];
int val[MAXN],vis[MAXN],ans;
int dui[MAXN],head,tail,f[MAXN]; inline int getint(){
int w=,q=; char c=getchar(); while((c<''||c>'') && c!='-') c=getchar();
if(c=='-') q=,c=getchar(); while (c>=''&&c<='') w=w*+c-'',c=getchar(); return q?-w:w;
} inline void read(){
int u=,now; char c;
do{
c=getchar(); if(c=='\n' || c=='\r') break;
now=c-'a'; if(!tr[u][now]) tr[u][now]=++cnt;
u=tr[u][now];
}while(c!='-');
val[u]++;
} inline void getfail(){
head=tail=; dui[++tail]=; int u,now,j;
while(head<tail) {
head++; u=dui[head];
for(int i=;i<;i++) {
if(tr[u][i]) {
now=tr[u][i]; j=f[u];
while(j && tr[j][i]==) j=f[j];
f[now]= tr[j][i]==now ? :tr[j][i];
dui[++tail]=now;
}
else tr[u][i]=tr[f[u]][i];
}
}
} inline void find(){
char c; int u=,now;
while(c=getchar()) {
if(c=='\n' || c=='\r') break;
now=c-'a'; u=tr[u][now];
vis[u]=;
}
for(int i=tail;i>=;i--) {
u=dui[i];
ans+=vis[u]*val[u];
vis[f[u]]|=vis[u];
}
} inline void work(){
int T=getint();
while(T--) {
cnt=; memset(tr,,sizeof(tr)); memset(vis,,sizeof(vis)); memset(val,,sizeof(val));
memset(f,,sizeof(f)); n=getint(); for(int i=;i<=n;i++) read();
getfail(); ans=;
find();
printf("%d\n",ans);
}
} int main()
{
work();
return ;
}
HDU2222 Keywords Search的更多相关文章
- 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 ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 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 ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
- JSOI2012 玄武密码 和 HDU2222 Keywords Search
玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...
随机推荐
- java多线程系类:JUC原子类:02之AtomicLog原子类
概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...
- make的控制函数(error,warning)
make的控制函数 make提供了两个控制make运行方式的函数.通常它们用在Makefile中,当make执行过程中检测到某些错误是为用户提供消息,并且可以控制make过程是否继续. 8.11.1 ...
- windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 解决方案
win7 MySql5.6.17提示:1045 access denied for user 'root'@'localhost' using password yes 从网上找到的解决方法,以此博客 ...
- 需要安全认证的远程EJB调用示例(Jboss EAP 6.2环境)
一,Remote EJB 服务接口定义: package yjmyzz.ejb.server.helloworld; public interface HelloWorldService { publ ...
- Spring Security授权 AccessDecisionManager
Spring Security授权 AccessDecisionManager 博客分类: Security Spring 在前面那篇博客有一段配置: <http auto-config=& ...
- Android -- Apk安装简诉
安装涉及到如下几个目录 system/app 系统自带的应用程序,无法删除 data/app 用户程序安装的目录,有删除权限. 安装时把apk文件复制到此目录 data/data 存放 ...
- [MetaHook] GameUI hook
Hook GameUI function. #include <metahook.h> #include <IGameUI.h> IGameUI *g_pGameUI = ; ...
- 翻译qmake文档(四) Building Common Project Types
翻译qmake文档 目录 本章原英文文档:http://qt-project.org/doc/qt-5/qmake-common-projects.html 构建常见的项目类型 本章描述 ...
- XML-RPC远程方法调用
一.简介 XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用. 它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列 ...
- STL中algorithm里的查找
首先,选择查找算法时,区间是否排序是一个至关重要的因素.可以按是否需要排序区间分为两组: A. count,find B. binary_search,lower_bound,upper_bound, ...