hdu_2222_Keywords Search(AC自动机板子)
存个自己写的AC自动机
#include<cstdio>
#include<cstring>
#define F(i,a,b) for(int i=a;i<=b;i++) const int AC_N=*,tyn=;//数量乘串长,类型数量
struct AC_automation{
int tr[AC_N][tyn],cnt[AC_N],Q[AC_N],fail[AC_N],tot;
inline int getid(char x){return x-'a';}
void nw(){cnt[++tot]=;memset(tr[tot],-,sizeof(tr[tot]));}
void init(){tot=-,fail[]=-,nw();}
void insert(char *s,int x=){
for(int len=strlen(s),i=,w;i<len;x=tr[x][w],i++)
if(tr[x][w=getid(s[i])]==-)nw(),tr[x][w]=tot;
cnt[x]++;//串尾标记
}
void build(int head=,int tail=){
for(Q[++tail]=;head<=tail;){
for(int i=,x=Q[head++],p=-;i<tyn;i++)if(~tr[x][i]){
if(x==)fail[tr[][i]]=;
else for(p=fail[x],fail[tr[x][i]]=;~p;p=fail[p])
if(~tr[p][i]){fail[tr[x][i]]=tr[p][i];break;}
Q[++tail]=tr[x][i];
}
}
}
int ask(char *s,int ans=){
for(int len=strlen(s),w,i=,x=,j;i<len;i++){
while(tr[x][w=s[i]-'a']==-&&x)x=fail[x];
x=tr[x][w],x=(~x)?x:,j=x;//加cnt后标记为0,防止重复计数
while(j&&cnt[j])ans+=cnt[j],cnt[j]=,j=fail[j];
}
return ans;
}
}AC;
char buf[];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
AC.init();
F(i,,n)scanf("%s",buf),AC.insert(buf);
AC.build();
scanf("%s",buf);
printf("%d\n",AC.ask(buf));
}
return ;
}
hdu_2222_Keywords Search(AC自动机板子)的更多相关文章
- 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 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 ...
- AC自动机板子题/AC自动机学习笔记!
想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...
- 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 ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
随机推荐
- Python 学习笔记10
念念不忘,必有回响. 今天继续学习Python 类.
- 小菜鸟安装CocoaPods
刚来到公司,以前没有用过CocoaPods. 参考的以下两篇文章,都是转载的. 第一篇比较偏技术性,叫做<Mac下CocoaPods安装步骤> http://blog.csdn.net/a ...
- ecshop php5.4以上版本错误之preg_replace 替换成 preg_replace_callback
类似这样的报错: Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instea ...
- Linux内核协议栈 NAT性能优化之FAST NAT
各位看官非常对不起,本文是用因为写的,如果多有不便敬请见谅 代码是在商业公司编写的,在商业产品中也不能开源,再次抱歉 This presentation will highlight our ef ...
- PHP实现中文截取无乱码
字符串的处理是编程中比较常见的,各种编程语言对字符串的处理也提供了大量函数,像php中mb_substr()函数可以实现对中文字符串的截取,如何使用自定义方法实现中文字符串截取无乱码这也是面试经常遇到 ...
- scanf函数与scanf_s函数
ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露.所以vc++2005/2008中提供了scanf_s(),在最新的VS2013中也提 ...
- OpenVPN客户端解析
windows版本的VPN客户端,实际上就是一个外壳,创建了图形界面,托盘,和 右键菜单, 在connect的动作里,实际上是通过cmd调用 openvpn.exe openvpn --config ...
- js常用函数大全107个
1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...
- JavaScript高级程序设计:第十七章
错误处理与调试 一.错误处理 1.try-catch语句: ECMA-262第3版引入了try-catch语句,作为javascript中处理异常的一种标准方式.基本的语法如下: try { //可能 ...
- LeetCode OJ 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...