Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50435    Accepted Submission(s): 16233

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
 
Author
Wiskey

把kmp和trie树组合起来,就是AC自动机。利用fail指针在失配时,在trie树中寻找上一个匹配点

目前并没有完全弄懂。理解大概还要好久

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct ACa
{
int next[][];
int fail[],end[];
int root,cnt;//根结点,计数器
int newnode(){
for(int i=;i<;i++){
next[cnt][i]=-;//子结点设为空
}
end[cnt++]=;//以该结点结束的串数
return cnt-;
}
int clear(){//初始化
cnt=;//结点数重置
root=newnode();//初始化根结点 //完成后root=0
}
int insert(char c[]){
int now=root;
int len=strlen(c);
for(int i=;i<len;i++){
if(next[now][c[i]-'a']==-)//如果对应结点未建立,添加结点
next[now][c[i]-'a']=newnode();
now=next[now][c[i]-'a'];//否则沿结点继续
}
end[now]++;
}
void build(){
queue<int>q;
fail[root]=root;
for(int i=;i<;i++){
if(next[root][i]==-)
next[root][i]=root;
else{//有结点则入队
fail[next[root][i]]=root;
q.push(next[root][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++){
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];//链接到fail指针对应结点的后面
else{
fail[next[now][i]]=next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
int query(char c[]){//查询
int len=strlen(c);
int now=root;
int res=;
for(int i=;i<len;i++){
now=next[now][c[i]-'a'];
int temp=now;
while(temp!=root)
{
res+=end[temp];
end[temp]=;
temp=fail[temp];
}
}
return res;
}
};
char buf[];
ACa ac;
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
ac.clear();
int i;
for(i=;i<=n;i++){
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}

HDU2222 Keywords Search [AC自动机模板]的更多相关文章

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

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

  2. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  3. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  4. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  5. hdu2222 Keywords Search ac自动机

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

  6. HDU2222 Keywords Search —— AC自动机

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

  7. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

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

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

随机推荐

  1. java 21 - 13 IO流之 合并流

    SequenceInputStream :表示其他输入流的逻辑串联. 构造方法摘要 SequenceInputStream(Enumeration<? extends InputStream&g ...

  2. java 20 - 4 IO流概述和一个简单例子解析

    IO流的分类:  流向: 输入流 读取数据  输出流 写出数据 数据类型:  字节流  字节输入流 读取数据 InputStream  字节输出流 写出数据 OutputStream  字符流  字符 ...

  3. [转] Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

    from:  http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得 ...

  4. [知乎] 刚开始学习 iOS 开发有什么书推荐呢?

    http://www.zhihu.com/question/19649679 我来说说我学习的过程:(无耻的使用一个回答 回答了很多问题.^^) 第一步,精读 Stephen G. Kochan 的& ...

  5. 17SpringMvc_在业务控制方法中写入包装User的模型来收集参数——解决问题

    在解决问题之前,我要说明一下jsp页面上填入信息,一个用户的信息比如用户的名字,用户的电话,用户的手机等等,在这个jsp页面上填好信息后,转到有个action处理这个信息.原理是什么? 在jsp页面上 ...

  6. 解决服务器连接错误Host ‘XXX’ is not allowed to connect to this MySQL server

    这段时间在研究火车头的入库教程,在“配置登陆信息和数据库(mysql)”连接中,出现“服务器连接错误Host 'XXX' is not allowed to connect to this MySQL ...

  7. OpenGL2.0及以上版本中glm,glut,glew,glfw,mesa等部件的关系

    OpenGL2.0及以上版本中gl,glut,glew,glfw,mesa等部件的关系 一.OpenGL OpenGL函数库相关的API有核心库(gl),实用库(glu),辅助库(aux).实用工具库 ...

  8. html 空格-有趣的试验

    首先,先给大家看一组demo <input /> <input type="submit" /> 展示效果: 为什么会出现空格呢?input不是行内元素吗? ...

  9. CSS元素分类

    快级元素:在html中<div>,<p>,<h1>,<form>,<ul>,<li>就是块级元素.                ...

  10. Command Pattern -- 命令模式原理及实现(C++)

    主要参考<大话设计模式>和<设计模式:可复用面向对象软件的基础>两本书.本文介绍命令模式的实现. What it is:Encapsulate a request as an ...