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. Getting Started with Testing ——开始单元测试

    Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as i ...

  2. vs在winform中不给力哈-错误不提示

    我的操作系统是windows Server 2008 x64,运行winform的时候,对Dictionary累加值.运行的时候,项目一闪而过,于是我在Project的Properties上选择运行的 ...

  3. Dell服务器MegaCli命令只返回Exit Code: 0x00问题分析

    今天同事给我说一台dell的服务器做了raid后,使用MegaCli看不到raid信息,上去看了一下确实不返回任何raid信息,但是确实机器上做了raid. 这就奇怪了,然后把MegaCli升级到最新 ...

  4. power desinger 学习笔记<一>

    如果一张表有 很多字段(多于30个),那么一个一个复制粘贴,耗时耗力.可以偷懒,事先编辑好 sql脚本,然后把sql脚本导入 power designer,是不是很方便?  看下面的 1. 打开Pow ...

  5. iOS目录结构

    默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 Documents:苹果建议将程序中建立的或在程序中浏览到的文件 ...

  6. SGU 186.The Chain

    看懂题就是水题... code #include <iostream> #include <algorithm> using namespace std; int a[110] ...

  7. print 函数的进一步理解

    没有括号的时候,pritn是列表操作符,会把其后列表里所有东西都数出来. 但是假如print后面紧跟着左括号,它就是一个函数调用,只会将括号内的东西输出来. “假如它看起来像函数调用,它就是一个函数调 ...

  8. 80端口被占用 PID = 4解决办法

    请按照下面的步骤来运行命令:1. sc config http stat = demand2. reboot3. run the command(netsh http show servicestat ...

  9. TCP协议握手与分手

    TCP(Transmission Control Protocol) 传输控制协议 TCP的7次握手可以理解为3次握手和4次分手. TCP状态转换图,如下: 这个图N多人都知道,它对排除和定位网络或系 ...

  10. tail tailf 使用

    tail -f tailf 用来查看日志的新增内容, tailf 能一直打印日志