本文版权归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. CSS3 perspecitve属性

    M M .div1 { position: relative; height: 150px; width: 150px; margin: 0px; padding:2px; border: 1px s ...

  2. NET WebApi OWIN 实现 OAuth 2.0

    NET WebApi OWIN 实现 OAuth 2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和 ...

  3. linux numfmt 命令--转换数字

    numfmt SYNOPSIS numfmt [OPTION]... [NUMBER]... DESCRIPTION Reformat NUMBER(s), or the numbers from s ...

  4. QT QDateTime类、QTimer类

    QDateTime类,头文件#include <QDateTime> 可以使用QDateTime类来获得系统时间.通过QDateTime::currentDateTime()来获取本地系统 ...

  5. U3D屏幕空间到世界空间变换

    using UnityEngine; using System.Collections; public class FPSCam : MonoBehaviour { Vector3 lastPos; ...

  6. python数字图像处理(15):霍夫线变换

    在图片处理中,霍夫变换主要是用来检测图片中的几何形状,包括直线.圆.椭圆等. 在skimage中,霍夫变换是放在tranform模块内,本篇主要讲解霍夫线变换. 对于平面中的一条直线,在笛卡尔坐标系中 ...

  7. web文档在线阅览

    之前遇到很多各种文档在线阅览的需求,也有不少朋友经常问我这种需求的实现方案,大致试了一下网上的一些比较主流的推荐方案,但都不尽如人意,这里有一个比较全面的总结,需要的朋友可以根据自己的需求到这里查看, ...

  8. JavaScript学习笔记-元素在滚动条滑动一定高度后自动置顶

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. Android下常见动画

    摘要:Android中常见的的动画有三种:属性动画.补间动画.帧动画. 注.因为前两种内容较多,后补 一.属性动画 二.补间动画 三.帧动画:本质是将一些连贯的图片加载形成连贯的动画效果 1.在Dra ...

  10. 转 ext文件系统及块组

    一.文件系统概述 1. 引导块 前文中介绍过磁盘需要进行分区和格式化,才能创建文件系统并使用,那么一块已经被各式化了分区其结构是什么样的呢?分区是按照柱面来划分的,而柱面包含的是磁道,磁道上包含的是扇 ...