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

Sample Output

3


AC自动机模板,加一个小优化,在处理失配指针的时候预处理节点经过失配后到达的节点


#include<bits/stdc++.h>
using namespace std;
#define N 1000010
struct Node{
int fail,cnt,ch[26];
void clean(){
fail=cnt=0;
memset(ch,0,sizeof(ch));
}
}t[N];
int tot,n,ans;
bool vis[N];
char s[N];
void init(){
tot=1;ans=0;
memset(vis,0,sizeof(vis));
t[0].clean();t[1].clean();
for(int i=0;i<26;i++)t[0].ch[i]=1;
}
int index(char c){return c-'a';}
void insert(char *s){
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int tmp=index(s[i]);
if(!t[u].ch[tmp])
t[t[u].ch[tmp]=++tot].clean();
u=t[u].ch[tmp];
}
t[u].cnt++;
}
void buildFail(){
static queue<int> q;
q.push(1);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<26;i++){
int w=t[u].ch[i],v=t[u].fail;
while(!t[v].ch[i])v=t[v].fail;
v=t[v].ch[i];
if(w){
t[w].fail=v;
q.push(w);
}else t[u].ch[i]=v;//失配预处理
}
}
}
void collect(int u){
while(u&&!vis[u]){
vis[u]=1;
ans+=t[u].cnt;
u=t[u].fail;
}
}
int main(){
int T;scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
insert(s);
}
buildFail();
scanf("%s",s);
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int tmp=index(s[i]);
u=t[u].ch[tmp];
collect(u);
}
printf("%d\n",ans);
}
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】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

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

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

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

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

  10. Keywords Search AC自动机

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

随机推荐

  1. Codeforces Round #414 C. Naming Company

    http://codeforces.com/contest/794/problem/C 题意: 有两个人要为公司起名字,每个人手中都有n个字符,现在要取一个n个字符长度的公司名.两人轮流取名,每次选择 ...

  2. 关于Eclipse SVN 分支 与主干 小结

    SVN建立分支和合并代码 https://blog.csdn.net/luofeixiongsix/article/details/52052631 SVN创建指定版本号的分支 https://blo ...

  3. windows向github提交代码

    随便写的,留给自己看. 一.在github上注册并建立自己的仓库http://www.cnblogs.com/keZhenxu94/p/5288488.html 二.安装windows版本git界面工 ...

  4. awardRotate.js

    (function($) { var supportedCSS,styles=document.getElementsByTagName("head")[0].style,toCh ...

  5. C# WebSocket解析(收发数据包、分片超长包处理)

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  6. 一些putty的应用设置

    把windows下putty的key转换成linux上使用的rsa key    http://ask.apelearn.com/question/937 putty生成的密钥导入connectbot ...

  7. 【Raspberry Pi】 小问题汇总

    注: 此系列为自己之前所搭建网站内容. 目前入手树莓派2,将遇到的一些琐碎的问题记录在此. 1. 更改时区 查看日期命令:date 输入sudo dpkg-reconfigure tzdata后按提示 ...

  8. iOS自动化探索(九)使用Jenkins自动化打包并发布iOS App

    继前一篇: Mac环境下安装Jenkins Jenkins安装好后, 我们试着创建一个iOS自动打包并发布的任务 iOS App构建必须在MAC上面使用xcode进行,所以我们要安装下xcode集成插 ...

  9. http请求的GET和POST请求:查询和新增(server.php)

    <?php //设置页面内容是html编码格式是utf-8 header("Content-Type: text/plain;charset=utf-8"); //heade ...

  10. yum的搭建

    搭建本地yum仓库的步骤 . 创建光盘目录,挂载光盘 . 进入/etc/yum/repos.d目录下,备份所有配置文件 . 利用一个含有大写M的配置文件作为配置文件的模板 . 在模板里将enabled ...