【HDU2222】Keywords Search

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

Smple Output

3

题意:给出一堆单词和一个字符串,问有多少个单词在字符串中出现过。

题解:AC自动机第一道模板题,我没有用指针,结果也AC了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int ch[26],fail,cnt;
}p[250000];
int n,tot,len,ans;
char str[1000010],w[60];
queue<int> q;
void build() //构建fail指针
{
int i,u,t;
q.push(1);
while(!q.empty())
{
u=q.front(),q.pop();
for(i=0;i<26;i++)
{
if(!p[u].ch[i]) continue;
q.push(p[u].ch[i]);
if(u==1)
{
p[p[u].ch[i]].fail=1; continue;
}
t=p[u].fail;
while(!p[t].ch[i]&&t) t=p[t].fail;
if(t) p[p[u].ch[i]].fail=p[t].ch[i];
else p[p[u].ch[i]].fail=1;
}
}
}
void search() //查找
{
int i,j,u,t;
scanf("%s",str);
len=strlen(str);
u=1;
ans=0;
for(i=0;i<len;i++)
{
while(!p[u].ch[str[i]-'a']&&u!=1) u=p[u].fail;
u=p[u].ch[str[i]-'a'];
t=u=(u>0)?u:1;
while(t!=1)
{
if(p[t].cnt>=0) ans+=p[t].cnt,p[t].cnt=-1; //防止重复计数
else break;
t=p[t].fail;
}
}
printf("%d\n",ans);
}
void work()
{
scanf("%d",&n);
tot=1;
memset(p,0,sizeof(p));
int i,j,k,t;
for(i=1;i<=n;i++)
{
scanf("%s",w);
k=strlen(w);
t=1;
for(j=0;j<k;j++) //构建Trie树
{
if(!p[t].ch[w[j]-'a']) p[t].ch[w[j]-'a']=++tot;
t=p[t].ch[w[j]-'a'];
}
p[t].cnt++;
}
build();
search();
}
int main()
{
int T;
scanf("%d",&T);
while(T--) work();
return 0;
}

【HDU2222】Keywords Search AC自动机的更多相关文章

  1. hdu2222 Keywords Search ac自动机

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

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

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

  3. HDU2222 Keywords Search —— AC自动机

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

  4. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  5. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  6. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  7. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  8. Keywords Search(AC自动机模板)

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

  9. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. telnet 传输文件

    用python创建个临时服务器,发送文件供telnet访问 import socket import base64 port = 10005 filename = 'libcrypto.so.1.0. ...

  2. 我理解的Sitecore开发流程

    Sitecore是一个强大的支持快速开发CMS建站的平台,进入sitecore后台可以看到 它主要有3个元素: 1.Templates模板 Data template:类似于面向对象编程中的类或结构体 ...

  3. jQuery 顺便学习下CSS选择器 奇偶匹配nth-child(even)

    今天学习jQuery,看到nth-child(even)用法,特意找了下这个选择器的用法,在CSS3标准中,用法很强大. 对此,我把CSS3标准中nth-child()用法大致介绍下: CSS3伪类选 ...

  4. kernel 4.4.12 外部模块Makefile 脚本编写

    kernel 4.4.12 最简单module的编译 上一篇博客上面有一个最简单的模块源代码,今天就上一个Makefile,运行make 就可以编译一个外部的模块. vim Makefile 这个是我 ...

  5. JSPatch热更新的利器.

    如果用一句话来描述JSPatch,就是利用系统自带的JavaScriptCore.framework配合RunTime机制,进行实时的代码下载与运行.. 而且使用也很简单,启动,加载JS,运行... ...

  6. find命令

    http://www.jb51.net/os/RedHat/1307.html find 目录(.代表当前目录) -type d -name "..."    f -name &q ...

  7. php mysqli mysqli_query() mysqli_real_query()

    2016年11月26日 15:22:27 星期六 场景: PHP从mysql中读取数据 1. 一次性读取所有数据返给PHP 2. 每次循环只读取一掉记录 数据量小的时候可以使用第一种方法, 数据量很大 ...

  8. 20. Valid Parentheses

    1.题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  9. asp.net 网页中播放 flash 和flv

    需求:在网页中播放powerpoint保存的pps文件和mp4文件 经过查阅:发现网页上直接播放pps文件比较麻烦(office web apps server),所以通过工具,将pps文件转换为sw ...

  10. sed 命令

    使用sed操作: .个人博客的文件,只输出学生姓名 .txt .txt .只输出每个学生的url .txt .只输出个人博客里的学号 .txt .只输出个人博客中,两个字姓名的学生名 .txt .只输 ...