Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27138    Accepted Submission(s): 8871

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自动机,模板题:

 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std;
typedef struct abc
{
abc *next[];
int end;
abc *fail;
}abcd;
int ans;
abcd *inti()
{
abcd *t;
t=(abcd *)malloc(sizeof(abcd));
t->end=;
t->fail=NULL;
for(int i=;i<;i++)
t->next[i]=NULL;
return t;
}
void insert(abcd *t,char z[])
{
if(*z=='\0')
{
t->end++;
return ;
}
if(t->next[*z-'a']==NULL)
t->next[*z-'a']=inti();
insert(t->next[*z-'a'],z+);
}
void ac(abcd *t)
{
queue< abcd* >a;
abcd *r,*f;
while(!a.empty())a.pop();
for(int i=;i<;i++)
{
if(t->next[i]!=NULL)
a.push(t->next[i]),t->next[i]->fail=t;
else
t->next[i]=t;
}
while(!a.empty())
{
r=a.front();
a.pop();
for(int i=;i<;i++)
{
if(r->next[i]!=NULL)
{
a.push(r->next[i]);
f=r->fail;
while(f->next[i] == NULL) f = f->fail;
r->next[i]->fail=f->next[i];
}
}
}
}
void query(abcd *t,char x[])
{
abcd *p = t, *f;
int i;
while(*x)
{
i = *x - 'a';
while(p->next[i] == NULL) p = p->fail;
p = p->next[i];
f = p;
while(f != t && f->end !=-)
{
ans+=f->end;
f->end =-;
f = f->fail;
}
x++;
}
}
void del(abcd *t)
{
for(int i=;i<;i++)
{
if(!t->next[i])
del(t->next[i]);
}
free(t);
}
int main()
{
int t,n,i;
cin>>t;
char z[];
char x[];
while(t--)
{
abcd *t;
t=inti();
cin>>n;
for(i=;i<n;i++)
{
cin>>z;
insert(t,z);
}
cin>>x;
ac(t);
ans=;
query(t,x);
cout<<ans<<endl;
del(t);
}
}

hdu2222 ac自动机入门的更多相关文章

  1. HDU2222(AC自动机入门题)

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

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

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

  3. hdu2222之AC自动机入门

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

  4. AC自动机入门

    Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...

  5. HDU 2222 Keywords Search(AC自动机入门)

    题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...

  6. HDU2222 (AC自动机)

    AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...

  7. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  8. hdu 1277 AC自动机入门(指针版和数组版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...

  9. hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

    /** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...

随机推荐

  1. Spring之声明式事务

    在讲声明式事务之前,先回顾一下基本的编程式事务 编程式事务: //1.获取Connection对象 Connection conn = JDBCUtils.getConnection(); try { ...

  2. C#输出杨辉三角形

    话不多说直接上代码: class Program { static void Main(string[] args) { ;//杨辉三角形的长度 Console.Write("输入杨辉三角长 ...

  3. tween.js的使用

    前面的话 TweenJS提供了一个简单但强大的渐变界面.它支持渐变的数字对象属性&CSS样式属性,并允许链接补间动画和行动结合起来,创造出复杂的序列.本文将详细介绍tween.js的使用 概述 ...

  4. select 标签选中 jquery

    网上很多错误 的[text ='xxx'] ,我晕. option 标签没有 text 属性.所以[]选取不到. 正确的: $("#yearCardTypeState option:cont ...

  5. jdk源码研究1-HashMap

    今天开始,研读下jdk的常用类的一些源码,下面是jdk中HashMap的研究.诚然,网上已经很多这方面的总结了,但是,个人只是想单纯地把自己的理解过程进行记录,大牛们就绕路吧,当然,欢迎扔砖头.下面是 ...

  6. 网络传输编程之TCP

    网络传输编程之TCP   网络传输编程指基于各种网络协议进行编程,包括TCP编程,UDP编程,P2P编程.本节介绍TCP编程.     (1)TCP简介: TCP是TCP/IP体系中最重要的传输层协议 ...

  7. 【1414软工助教】博客链接和coding链接

    某些同学提供的coding.net用户名无法访问.请同学们自己点击自己的两个链接,如果发现有错,请在本博客的评论区给出正确的链接. 格式为: 学号后3位 链接 例如:***502 https://co ...

  8. 个人作业2————英语学习APP的案例分析

    必应词典案例分析 第一部分 调研, 评测 1.下载并使用 第一次使用必应词典,安装完打开便是这样的界面,第一印象还行,界面平平无奇,比较简洁,上面分四个模块,这样一眼看去感觉功能比较单一 使用了下例句 ...

  9. 24点游戏详细截图介绍以及原型、Alpha、Beta对比

    原型设计 图片展示 功能与界面设计 1.登录注册 2.手机号验证 3.24点游戏 4.粉色系女生界面 Alpha 图片展示 功能与界面设计 1.24点游戏 2.背景音乐 3.可查看多种可能的答案 4. ...

  10. 团队作业4——第一次项目冲刺(Alpha版本)2nd day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 界面 1.四个用户登录界面已经完成. 2.界面内的功能完成了一小部分. 登陆部分 1.QQ授权已经申请,还未通过. 2.通过好 ...