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. object转化为string

    package common; import java.util.ArrayList; import java.util.List; public class DataZh { public stat ...

  2. JQ 让光标在文本框最末尾

    function setFocus() { //文本末尾获得焦点 var obj = event.srcElement; var txt = obj.createTextRange(); txt.mo ...

  3. 如何配置visual studio 2013进行负载测试-万事开头难

    声明:工作比较忙,文章写得不好,有时间再整理. 起因:最近众包平台因迁移到azure之后一直有网站慢的情况,让老板挨批了,但是测试环境一切正常,而且生产环境也没发现有卡顿和慢的情况,所以干脆来一次负载 ...

  4. oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断

    oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...

  5. JSONKit 在iOS9 arm7 64位下出现的问题

    最近遇到了一个关于JSONKit的问题,在项目加了arm7 64位以后,JSONKIT会出现[params JSONString] forKey:@”gson”];报错的情况,如下图 具体原因不太清楚 ...

  6. express 安装与卸载

    卸载: npm uninstall -g express安装指定版本: npm install -g express@3.5.0查看版本: express -V注意express -V中的V要大写,不 ...

  7. java中的继承要点

    java的一大特性既是:继承. 1.因为有了一个子类继承了一个父类,才有了后面的多态. 2.类的继承,不要为了节省代码,为了继承而继承,把那个没有任何相关的类链接在一起,继承必须用在 is a,就是例 ...

  8. system.getProperties()

    Properties props=System.getProperties(); //系统属性     System.out.println("Java的运行环境版本:"+prop ...

  9. PHP框架_Smarty

    目录 1.环境搭建 2.基本配置 3.Smarty变量调节器 4.Smarty条件判断 5.Smarty的循环 6.Smarty模板的引用 7.Smarty类与对象的赋值与引用 8.smarty函数插 ...

  10. yii2源码学习笔记(五)

    Event是所有事件类的基类.它封装了与事件相关的参数. yii2\base\Event.php <?php /** * @link http://www.yiiframework.com/ * ...