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的题解 将模式串建成一个 ...
随机推荐
- 传递多个参数并获取Web API的数据
近段时间学习Web Api觉得非常有意思.默认的路由情况之下,获取数据时,它不必指定Action操作名. 还有另外感想,就是自从学习asp.net MVC之后,加上jQuery,让Insus.NET已 ...
- Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件)
Cordova - 使用Cordova开发iOS应用实战3(添加Cordova控制台插件) 前文介绍了通过 Safari 的 Web检查器,可以看到控制台输出的信息.但有时这样调试代码不太方便,如果在 ...
- myeclipse中发送邮件出现Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
出现这个问题的原因是jar包版本不统一,解决方法如下: 我在项目导入了jar包 与myeclipse自带jar冲突了 删除Java EE 5 Libraries/javaee.jar/mail里的包有 ...
- 深入理解计算机系统(2.7)---二进制浮点数,IEEE标准(重要)
2.6我们进行了二进制整数运算的最后一役,本次LZ将和各位一起进入浮点数的世界,这里没有无符号,没有补码,但是有各种各样的惊奇.倘若你真正的进入了浮点数的世界,一定会发现它原来是这么有意思,而不是像之 ...
- [Codevs 1421]秋静叶&秋穣子(最大-最小博弈)
题目:http://codevs.cn/problem/1421/ 分析:有向树上的最大-最小博弈 先手与后手的策略不同: 先手A:让对方取得尽量少的前提下,自己取得尽量大 后手B:让自己取得尽量多的 ...
- [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...
- 63-w 简明笔记
显示关于系统用户的信息 w [options] [username] w用于显示当前登录系统的用户的名字以及他们的终端设备编号.登录时间.正在运行的命令和其他一些信息 参数 username 限定仅显 ...
- linux 下配置 nodejs+ionic+cordova
ionic是目前比较火的hybird框架学的人挺多所以资料会相对全一些. cordova是一个连接ionic和原生android 底层api的工具.(这样说好理解一些,不过可能不够准确.) 用他们的好 ...
- python基础-range用法_python2.x和3.x的区别
#range帮助创建连续的数字,通过设置步长来指定不连续 python2.7 #直接就在内存中创建出来(0-99) >>> range(100)[0, 1, 2, 3, 4, 5, ...
- Beta版本冲刺Day6
会议讨论: 628:配置Mysql的时候遇到了问题,在修改数据库用户密码时无法修改,并且服务器好像连接不上去了,其他组员则继续他们的任务.601:将一些原来的界面进行了修改,修改成了更加美观的外形. ...