HDU2222:Keywords Search——题解
http://acm.hdu.edu.cn/showproblem.php?pid=2222
题目大意:
给定 n 个长度不超过 50 的由小写英文字母组成的单词,以及一篇长为 m 的文章,问有多少个单词在文章中出现了,多组数据。
——————————————————————
AC自动机裸题。
(C++AC,G++TLE我能说什么……)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int N=1e4+;
const int M=1e6+;
const int L=+;
struct trie{
int ed;
int a[];
int fail;
void clear(){
ed=fail=;
memset(a,,sizeof(a));
return;
}
}tree[N*L]={};
char s[M];
int cnt=;
inline void insert(){
int now=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
if(!tree[now].a[t]){
cnt++;
tree[now].a[t]=cnt;
tree[cnt].clear();
}
now=tree[now].a[t];
}
tree[now].ed++;
return;
}
int q[N*L];
void getfail(){
int r=;
//以下是对第一层的特殊处理
for(int i=;i<;i++){
if(tree[].a[i]!=){
tree[tree[].a[i]].fail=;
r++;q[r]=tree[].a[i];
}
}
for(int l=;l<=r;l++){
int u=q[l];
for(int i=;i<;i++){
if(tree[u].a[i]!=){
tree[tree[u].a[i]].fail=tree[tree[u].fail].a[i];
r++;q[r]=tree[u].a[i];
}else{
tree[u].a[i]=tree[tree[u].fail].a[i];
}
}
}
return;
}
int check(){
int now=,ans=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
now=tree[now].a[t];
for(int j=now;j&&tree[j].ed!=-;j=tree[j].fail){
ans+=tree[j].ed;
tree[j].ed=-;
}
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
tree[].clear();
cnt=;
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>s;
insert();
}
getfail();
cin>>s;
cout<<check()<<endl;
}
return ;
}
HDU2222:Keywords Search——题解的更多相关文章
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- JSOI2012 玄武密码 和 HDU2222 Keywords Search
玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...
- 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 ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
随机推荐
- OpenCV 3.0.0处理鱼眼镜头信息 - Fisheye camera model
此篇随笔主要参考OpenCV 3.0.0的官方文档翻译而来,主要用作理解OpenCV对鱼眼相机的标定.图像校正.3D重建功能的理解. 版权所有,转载请注明出处~ xzrch@2018.09.29 参考 ...
- java 浅复制 深复制
1.浅复制 只是复制引用,对引用的操作会影响之前复制的对象. 2.深复制 复制一个完全独立的对象,复制对象与被复制对象相互之间不影响. 只是概念性东西....
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- Selenium 入门到精通系列:四
Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- CPU设计学习-流水线
各种名词 标量流水线 超级流水线 超标量流水线与多发射技术 经典五级流水线 IF |Instruction Fetch,取指 ID |Instruction Decode,译码 EX |Execute ...
- Log Files
Description Nikolay has decided to become the best programmer in the world! Now he regularly takes p ...
- Java数组课程作业
设计思路:生成随机数,赋值给数组.再将其求和输出 程序流程图: 源程序代码: import javax.swing.JOptionPane; public class Test { public st ...
- Java版office文档在线预览
java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...
- Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...
- c++远征
---恢复内容开始--- 这两天初步接触了C++,抱着一种对这两个加号的理解的心态走进这门语言的学习. 1.mooc--慕课网c++课程链接:http://www.imooc.com/learn/34 ...