Keywords Search

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

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
 

【思路】

AC自动机模板的直接应用。

需要注意的有:

    1)     
要用val纪录该节点对应得单词数目,因为可能会有重复的串。

    2)     
Val累计入cnt后清0,print可能会重复遍历一个相同的单词。

【代码】

 #include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxn = +;
const int maxl = +;
const int maxnode = maxl*maxn;
const int sigma = ; struct ACautomaton {
int ch[maxnode][sigma];
int f[maxnode],val[maxnode],last[maxnode];
int sz,cnt; void clear() {
sz=; cnt=;
memset(ch[],,sizeof(ch[]));
val[]=f[]=;
}
int idx(char c) { return c-'a'; }
void insert(char* s) {
int n=strlen(s),u=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
}
void print(int j) {
if(j) {
cnt+=val[j];
val[j]=; //一定要置为0防止重复累计
print(last[j]);
}
}
void find(char* s) {
int n=strlen(s),j=;
for(int i=;i<n;i++) {
int c=idx(s[i]);
while(j && !ch[j][c]) j=f[j];
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
void get_Fail() {
queue<int> q;
f[]=;
for(int c=;c<sigma;c++) {
int u=ch[][c];
if(u) { f[u]=last[u]=; q.push(u); }
}
while(!q.empty()) {
int r=q.front(); q.pop();
for(int c=;c<sigma;c++) {
int u=ch[r][c];
if(!u) continue;
q.push(u);
int v=f[r];
while(v && !ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
}ac; int n; int main() {
//freopen("in.in","r",stdin);
//freopen("out_me.out","w",stdout);
int T;
scanf("%d",&T);
while(T--) {
char s[maxl];
scanf("%d",&n);
ac.clear();
FOR(i,,n) {
scanf("%s",s);
ac.insert(s);
}
ac.get_Fail();
char T[];
scanf("%s",T);
ac.find(T);
printf("%d\n",ac.cnt);
}
return ;
}

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. maven常见命令总结

    1.maven vm配置 -Xms512m -Xmx1024m -XX:PermSize=256m 2.启动tomcat  clean package -Dpackage.environment=de ...

  2. 11g 创建asm磁盘组

    [root@Oracle11g ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2 ...

  3. jQuery 效果 - 淡入淡出

    通过 jQuery,您可以实现元素的淡入淡出效果. 点击展示 淡入/淡出 面板 实例 jQuery fadeIn()演示 jQuery fadeIn() 方法. jQuery fadeOut()演示 ...

  4. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib 错误的解决办法

    查看项目属性,发现该项目的目标框架是.NET Framework 4 Client Profile ,而被引用的程序集的目标框架是.NET Framework 4,将该项目的目标框架修改成.NET F ...

  5. visualSVN server库迁移(转)

    转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...

  6. 一个IP支持多个网站实例(Apache2、Ubuntu相关)

    http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...

  7. easyui tree 判断点击的节点是否还存在子节点

    有些业务需求是要求tree一次性全部加载,有些是需要异步加载的. 如果是一次性全部加载的tree,那怎么判断点击的节点是否还存在子节点? function loadTree(){ $('#tree') ...

  8. ThinkPHP 笔记

    1.循环中使用比较运算符 <volist name="subjects" id="v">       <option value=" ...

  9. Yeoman:适合现代Web应用的现代工作流

    Yeoman:适合现代Web应用的现代工作流   Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML. ...

  10. 【转】JQUERY相关的几个网站

    作者:Terry li - GBin1.com 1. John Resig - http://ejohn.org 毫无疑问,jQuery 的缔造者的博客是你首先必须关注的. 2. Filmament ...