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. win10自动更新彻底关闭

    http://app.techweb.com.cn/wp/2016-10-24/2418646.shtml

  2. ubuntu 命令收集

    1. ctrl + Alt + F1:   进入纯粹的命令行. 2. ctr + Alt + T :    从图形界面打开终端.

  3. ajax载入数据是小细节

    今天看了一个点子: 在 ajax 导入数据的 div中添加一些样式,比如:我们正紧急抢救 增加趣味性,有解决数据卡壳问题

  4. Java常用类库——Runtime

    runtime运行时候,是封装了一个JVM进程的类,每一个JAVA程序实际上启动了一个JVM进程,那么每个JVM对应一个runtime实例.此实例是由JVM为其实例化. 本类的定义中没有构造方法,因为 ...

  5. 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)

    <style> html,body{margin:0;padding:0;} .d1{margin-left:40px;background:red;width:2000px;height ...

  6. 关于audio元素在实际项目中遇到的问题总结

    在ios高版本的微信浏览器下(ios10.0以上),audio标签如果添加autoplay属性的话.导致的问题是:通过二维码扫码第一次进入没有问题,第二次扫码进入之后直接卡死在loading页面. 解 ...

  7. Thread锁 Monitor类、Lock关键字和Mutex类

    Monitor 类锁定一个对象 当多线程公用一个对象时,也会出现和公用代码类似的问题,这种问题就不应该使用lock关键字了,这里需要用到System.Threading中的一个类Monitor,我们可 ...

  8. Windows下安装Redmine

    参考链接:http://www.cnblogs.com/afarmer/archive/2011/08/06/2129126.html 最新教程:http://www.myexception.cn/w ...

  9. 【Mysql】 my.ini配置一例

    # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...

  10. U3D assetbundle加载

    using UnityEngine; using System.Collections; public class testLoadFromAB : MonoBehaviour { IEnumerat ...