【刷题】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,那么当单词数量非 ...
随机推荐
- Python构建web应用(进阶版)->对网页HTML优化逻辑显示
本篇是承接上一篇web应用(入门级)的内容往下顺延的,阅读后将会了解HTML逻辑显示优化,如下图所示,从杂乱无章的日志文件到一个整齐的列表显示. —————————————————————————— ...
- 学习python,第一篇
name = "danie" name2 = name print(name,name2) name = "itxpl" print(name,name2) 结 ...
- Netty源码分析第5章(ByteBuf)---->第2节: ByteBuf的分类
Netty源码分析第五章: ByteBuf 第二节: ByteBuf的分类 上一小节简单介绍了AbstractByteBuf这个抽象类, 这一小节对其子类的分类做一个简单的介绍 ByteBuf根据不同 ...
- dp算法之有代价的最短路径
题目:有代价的最短路径 题目介绍:如下图所示,现在平面上有N个点,此时N=7,每个点可能和其他点相连,相连的线有一定权值,求出从0点到N-1点的消耗权值的最小值. 分析:用动态规划的思路来解决,每一点 ...
- 编写和调试Android下JNI程序流程
1,切换到Android目录下bin/classes,使用javah命令生成jni所需的头文件,命令类似于:javah com.xxx.ooo,其中,com.xxx为package名称,ooo为包含n ...
- 信息安全系统设计基础_exp1
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1353 姓名:吴子怡.郑伟 学号:20135313.20135322 指导教师: 娄嘉鹏 实验 ...
- java程序设计课程实验报告1
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:陈都 学号:20135328 成绩: 指导 ...
- 20162327WJH第二次实验——树
20162327WJH第一次实验--线性结构 实 验 报 告 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王旌含 学号:20162327 成绩: 指导教师:娄嘉鹏 王志强 实验 ...
- 《Spring1之 第一次站立会议(重发)》
< 第一次站立会议(重发)> 昨天,我对我们团队要做的项目进行了相关资料的查找,我找到了服务器和客户端的相关代码以及找到了把它们通信所使用TCP/IP等协议: 今天,我把找到的代码和协议资 ...
- 凡事预则立|项目Beta冲刺准备
1.讨论组长是否重选的议题和结论. 组员一致认为组长不需要重选,我们都很认可组长的表现,组长的付出我们都看在眼里,我们找不出更适合担任组长的人选. 2.下一阶段需要改进完善的功能. 财富值的布局优化以 ...