【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. 菜鸟笔记:javascript基础之表达式和运算符

    4.1 原始表达式 原始表达式是最简单的表达式~它不再包含其他表达式.它包含:直接量(程序中直接显示出来的数据值.)常量(程序中不会被修改的量)变量. 4.2 对象和数组的初始化表达式 对象和数组初始 ...

  2. Markdown 语法简要介绍

    =================MarkDown================= Markdown 是一种方便记忆.书写的纯文本标记语言,用户可以使用这些标记符号以最小的输入代价生成极富表现力的文 ...

  3. MAC实用的小工具

    一.XtraFinder(右键菜单扩展) http://www.xuebuyuan.com/173454.html http://www.mamicode.com/info-detail-111618 ...

  4. MySQL备份mydumper的原理

    本文来自:http://baiyangtx.net/2016/09/04/mydumper-principle/ 相对于MySQL官方提供的逻辑备份工具 mysqldump , mydumper最大的 ...

  5. theano学习

    import numpy import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar( ...

  6. iOS10 远程推送代码 以及服务器端代码(.net)

    // // AppDelegate.m // MyPushDemo // // Created by justapple on 16/12/25. // Copyright © 2016年 dengq ...

  7. android 决解启动屏白黑屏会延迟几秒的问题

    通常写启动屏,都有个很不喜欢的问题,就是会空白几秒才显示界面,而且界面还是很简单的! 解决办法 1 写一个透明的主题,一般启动屏都是不要bar的所以继承AppTheme.NoActionBar < ...

  8. 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。

    查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  9. Linux下删除文件的原理

    Linux下文件删除的原理 Lniux下控制文件真正被删除的计数器 Linux是link的数量来控制文件删除的.只有当一个文件不存在任何link的时候,这个文件才会被删除.一般来讲,每个文件都有两个l ...

  10. python之I/O操作

    IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. 比如你打开 ...