本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
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
First 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.
 

Output

Print how many keywords are contained in the
description.
 
Sample Input
1
5
she
he
say
shr
her
yasherhs
 
Sample Output
3
 

正解: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的更多相关文章

  1. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  3. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. hdu----(2222)Keywords Search(trie树)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU2222 Keywords Search(AC自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. hdu2222 Keywords Search【AC自动机】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  8. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  9. AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)

    首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...

  10. JSOI2012 玄武密码 和 HDU2222 Keywords Search

    玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...

随机推荐

  1. 【MySQL】使用trim函数删除两侧字符

    第一个LEADING,可以删除左侧指定的字符以及字符串 SELECT trim(LEADING '/' FROM `path`) 第二个TRAILING,可以删除右侧的指定字符以及字符串 SELECT ...

  2. WP老杨解迷:发布包多少大小合适

    有位做安卓的老兄这样描述发布包大小问题:发布包和女人一样,新包如年轻女子,不能太瘦,太瘦没有货,所以大家都喜欢身段窈窕的少女,正火的产品如中年妇女,要得是风韵魅力,胖瘦已经不那么重要,但是也不能太胖, ...

  3. 基于Spring的简易SSO设计

    通常稍微规模大一些的企业,内部已经有很多的应用系统,多个系统整合首先要解决的便是“统一登录(SSO)”问题,之前写过一篇 利用Membership实现SSO(单点登录) ,java环境下已经有一些开源 ...

  4. Spring Security笔记:自定义登录页

    以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...

  5. Java多线程之Runable与Thread

    Java多线程是Java开发中的基础内容,但是涉及到高并发就有很深的研究可做了. 最近看了下<Java并发实战>,发先有些地方,虽然可以理解,但是自己在应用中很难下手. 所以还是先回顾一下 ...

  6. 快速开发之代码生成器(asp.net mvc4 + easyui + knockoutjs)

    一.前言 作为一个码农这么多年,一直在想怎么提高我们的编码效率,关于如何提高编码效率,我自己的几点体会 1.清晰的项目结构,要编写代码的地方集中 2.实现相同功能的代码量少并且清晰易懂 3.重复或有规 ...

  7. 从语言到库到框架,再到API,再到标记最后到DSL语言

    计算机技术发展很快,而且越来越快,结果也是越来越复杂,那么我们到底怎么搞定复杂性并重用代码? 很明显,这是个大难题.一开始我们要解决计算问题,发展了基本的编程语言. 很快,编程语言不能满足需求,我们需 ...

  8. scrollHeight,scrollLeft,offsetHeight,offsetLeft

    scrollHeight:内部元素的绝对高度,包含内部元素的隐藏的部分scrollWidth:内部元素的绝对宽度,包含内部元素的隐藏的部分 scrollLeft:设置或获取位于对象左边界和窗口中目前可 ...

  9. 拼图游戏(js,C#,java三种语言)

    <html> <head> <meta charset="utf-8"> <style type="text/css" ...

  10. js 基础(一)

    <!--最近需要用到js相关的知识 就把在W3cSchool 下学到的东西做个笔记,方便以后再看 --><!DOCTYPE html> <html> <hea ...