HDU2222 Keywords Search(AC自动机)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 46676 Accepted Submission(s): 14858
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.
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.
5
she
he
say
shr
her
yasherhs
【思路】
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自动机)的更多相关文章
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
随机推荐
- PrintWriter 和 BufferedWriter 写入文件.
Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...
- TFS扩展开发中遇到的坑
本码农最近开发一个VS扩展,其中有些功能涉及到文件的签出.我们公司用的是TFS,遇到了一些奇特的现象,将解决过程记录如下. 一.明明在线的连接却Offline属性等于True public stati ...
- linux创建用户
创建用户 sudo adduser xxx 删除用户 sudo userdel xxx 删除用户和目录 sudo userdel -r xxx
- Strut2 采用token机制防御CSRF同时也可以防止表单重复提交
一 未配置Struts2 token的情况下测试 1.从表单提交数据,可以从下图看出,快速点击保存按钮,请求提交了两次 2.检查post提交的数据中未含有token参数 3.查看数据列表,有重复数据 ...
- 自己制作精美的App Store 软件截屏
当用户搜索到App的时候,一般都会先看截图,如果截图效果不好,可能用户就不会下载. 不想自己辛苦写的认为还不错的软件,因为截图的原因,而降低了很多下载量吧. 轻轻松松做出这样高大上的截屏效果来. Sc ...
- phpcms源码解析(2)
1.程序启动逻辑: 首先由文件\index.php调用create_app(),此函数在文件\phpcms\base.php中,它完成初始化应用程序,调用函数load_sys_class并提供参数ap ...
- [FindBugs分析记录]Redundant nullcheck of o,which is known to be non-null
官网解释: This method contains a redundant check of a known non-null value against the constant null. 这种 ...
- wordpress 更改 "Home"为"首页"
要怎麼更改wordpress的 menu上 那一直顯示著"首頁"的頁籤呢這問題我實在是找好久終於給我找到 在 wp-includes 的 post-template.php 這檔案 ...
- 【Linux】vi编辑器命令
1)进入vi的命令 vi filename : 打开或新建文件,并将光标置于第一行首 vi +n filename : 打开文件,并将光标置于第n行首 ...
- 转:SQL Case when 的使用方法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' EN ...