AC自动机模板题 HDU - 2222
Keywords Search HDU - 2222
贴个vj的链接https://vjudge.net/problem/HDU-2222
题意:T组数据,n个单词,再给你一个串,看有几个单词在这个串里面出现过
Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std; const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; const int N = 5e5 + 1000;
int fa[N], trie[N][30], tot, vis[N]; void build(string s){
int len = s.size();
int pos = 0;
for(int i = 0; i < len; i++){
int son = s[i] - 'a';
if(!trie[pos][son]) trie[pos][son] = ++tot;
pos = trie[pos][son];
}
vis[pos]++;
}
void getfail(){
queue<int> q;
for(int i = 0, p; i < 26; i++){
p = trie[0][i];
if(p){
fa[p] = 0;
q.push(p);
}
}
while(q.size()){
int now = q.front();
q.pop();
for(int i = 0; i < 26; i++){
int u = trie[now][i];
if(u){
q.push(u);
fa[u] = trie[fa[now]][i];
}
else trie[now][i] = trie[fa[now]][i];
}
}
} int getans(string s){
int now = 0, ans = 0;
for(int i = 0; i < s.size(); i++){
now = trie[now][s[i]-'a'];
for(int j = now; j && vis[j]!=-1; j = fa[j]){
ans+=vis[j];
vis[j] = -1;
}
}
return ans;
} int main(){
int t; cin >> t;
fa[0] = 0;
while(t--){
memset(trie, 0, sizeof(trie));
memset(vis, 0, sizeof(vis));
tot = 0;
int n; cin >> n;
string s;
for(int i = 1; i <= n; i++){
cin >> s;
build(s);
}
getfail();
cin >> s;
cout << getans(s) << endl;
}
}
AC自动机模板题 HDU - 2222的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- js正则 -180 到180 小数点后无限位、el-input
正则 -180 到180 小数点后无限位/^0$|^-?0\.\d*[1-9]$|^-?[1-9](\.\d*[1-9])?$|^-?[1-9]\d(\.\d*[1-9])?$|^-?1[0-7]\d ...
- Knowledge Fusion例子
#! NX/KF 5.0 # #************************************************************************************ ...
- Delphi实现Windows的气泡提示样式
其实也不复杂,主要使用到shellAPI下的TNotifyIconData这个结构体: typedef struct _NOTIFYICONDATA { DWORD cbSize; //结构体的大小, ...
- IQueryable 和 IEnumerable 的区别
讲一讲 IQueryable 和 IEnumerable 的区别. 我们会在使用 LINQ 查询方法之后,又使用 ToList 等方法,将查询结果转换成集合. 如果我们不使用 ToList 呢? 比如 ...
- Redis集群(主从复制)
主从复制主从复制原理 Redis集群中有很多Redis服务器,这些Reids服务器分为主服务器和从服务器. 从服务器会向主服务器发送命令:SYNC命令. 主服务器接收到SYNC命令后,开始执行GBSA ...
- 七牛云服务器debug
400{ResponseInfo:com.qiniu.http.Response@16fd3806,status:400, reqId:uzcAADGFlzHUE-kW, xlog:-, xvia:, ...
- 基于uniapp框架开发飞书小程序总结
前期准备 飞书官方客户端文档:https://open.feishu.cn/document/home/intro 飞书官方工具资源文档:https://open.feishu.cn/document ...
- Vue获取DOM的几种方法
虽然Vue实现了MVVM模型,将数据和表现进行了分离,我们只需要更新数据就能使DOM同步更新,但是某些情况下,还是需要获取DOM元素进行操作(比如引入的某个库要求传入一个根dom元素作为根节点,或者写 ...
- http hook
class XMLHttp { request = function(param) {} ; response = function(param) {} ; } let httpCopy = new ...
- vue中使用Echart将一组数据展示出三种统计图
1 <template> 2 <div class="container"> 3 <div id="myEchart" style ...