【刷题】HDU 2222 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
Description(CHN)
给 \(n\) 个模式串和一个文本串,求文本串中出现了多少个模式串
Solution
AC自动机裸题,做法跟【刷题】洛谷 P3808 【模板】AC自动机(简单版)一模一样
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10000+10,MAXM=1000000+10,MAXS=500000+10;
int T,n,ch[MAXS][30],fail[MAXS],ed[MAXS],cnt,vis[MAXS],last[MAXS],ps[MAXN];
char s[MAXM];
std::queue<int> q;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void init()
{
for(register int i=0,lt=strlen(s);i<lt;++i)s[i]-='a'-1;
}
inline void insert(int rk)
{
int x=0;
for(register int i=0,lt=strlen(s);i<lt;++i)
if(!ch[x][s[i]])x=ch[x][s[i]]=++cnt;
else x=ch[x][s[i]];
ed[x]=1;
ps[rk]=x;
}
inline void getfail()
{
for(register int i=1;i<=26;++i)
if(ch[0][i])q.push(ch[0][i]);
while(!q.empty())
{
int x=q.front();
q.pop();
for(register int i=1,y,z;i<=26;++i)
if(ch[x][i])
{
y=ch[x][i],z=fail[x];
while(z&&!ch[z][i])z=fail[z];
fail[y]=ch[z][i];
last[y]=ed[fail[y]]?fail[y]:last[fail[y]];
q.push(y);
}
else ch[x][i]=ch[fail[x]][i];
}
}
inline void save(int x)
{
if(x)
{
if(ed[x])vis[x]=1;
save(last[x]);
}
}
inline void match()
{
int x=0;
for(register int i=0,lt=strlen(s);i<lt;++i)x=ch[x][s[i]],save(ed[x]||last[x]?x:0);
}
int main()
{
read(T);
while(T--)
{
memset(ch,0,sizeof(ch));
memset(ed,0,sizeof(ed));
memset(ps,0,sizeof(ps));
memset(vis,0,sizeof(vis));
memset(fail,0,sizeof(fail));
memset(last,0,sizeof(last));
cnt=0;
read(n);
for(register int i=1;i<=n;++i)scanf("%s",s),init(),insert(i);
getfail();
scanf("%s",s);init();match();
int ans=0;
for(register int i=1;i<=n;++i)
if(vis[ps[i]])ans++;
write(ans,'\n');
}
return 0;
}
【刷题】HDU 2222 Keywords Search的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
随机推荐
- spring-boot2.x Application properties属性配置
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- numpy技巧
矩阵旋转: mat_array = np.array([[1,2],[4,3]]) rotate_90 = np.zeros((2,2), np.uint8) rotate_180 = np.zero ...
- Python自动化运维
一.DNS域名轮询业务监控 链接:https://www.cnblogs.com/baishuchao/articles/9128953.html 二.文件内容差异对比方法 链接:https://ww ...
- jQuery的$ .ajax防止重复提交的方法
没啥说的直接贴代码,很简单: 第一种方式:的onclick点击事件类型 <SCRIPT> function member_del(obj,id){ var lock = false; // ...
- Git常用使用技巧
- 此随笔不是使用教材,使用教材参照git官方文档和相应博客 - 此随笔不是转载而来,涉及不少三方链接,再次表示感谢 - 此随便乃实践中碰到的问题,增加开发效率,干货满满 git 撤销某次提交的技巧: ...
- PASSWORD MySQL 5.6.21-1ubuntu14.04_amd64
/***************************************************************************** The main idea is that ...
- React Native (0.57)开发环境搭建(注意:Node不要随便更新到最新版,更新完后莫名其妙的问题一大堆)
搭建开发环境 一.安装依赖 必须安装的依赖有:Node.Watchman 和 React Native 命令行工具以及 Xcode. 1.首先安装 Homebrew 2.安装 Node, Watchm ...
- 20162325 金立清 S2 W7 C16
20162325 2017-2018-2 <程序设计与数据结构>第7周学习总结 教材学习内容概要 树是非线性结构,其元素组织为一个层次结构 树的度表示树种任意结点的最大子结点数 有m个元素 ...
- DPDK flow_filtering 源码阅读
代码部分 main.c /*- * BSD LICENSE * * Copyright 2017 Mellanox. * * Redistribution and use in source and ...
- 使用fdisk创建好了分区,但是在生成物理卷出现"Device /dev/sdb2 not found (or ignored by filtering)."解决方法
生成物理卷出现的问题 解决方法 问题所在: 内核没有成功同步分区信息,所有才需要使用"partprobe"命令来手动同步