POJ2222 Keywords Search AC自动机模板
- #include<cstdio>
- #include<cstring>
- const int maxn=;
- const double eps=1e-;
- const long long modn=;
- int n;
- char a[]={};
- char b[*maxn]={};
- struct trie{
- int next[];
- bool exist;
- int count;
- int fail;
- }e[maxn*]={};
- int tot,ans;
- int q[maxn*]={};
- void init(int x,int k,int j){
- if(j>k){
- e[x].exist=;
- e[x].count++;
- return;
- }
- int z=a[j]-'a';
- if(!e[x].next[z])
- e[x].next[z]=++tot;
- init(e[x].next[z],k,j+);
- }
- void fir(){
- memset(e,,sizeof(e)); memset(q,,sizeof(q));
- tot=ans=;
- }
- void doit(){
- int head=,tail=,x,y,f;
- q[]=;
- while(head<=tail){
- x=q[head++];
- for(int i=;i<;i++){
- if(e[x].next[i]){
- y=e[x].next[i];
- if(x!=){
- f=e[x].fail;
- while((!e[f].next[i]) && f )
- f=e[f].fail;
- e[y].fail=e[f].next[i];
- }q[++tail]=y;
- }
- }
- }
- }
- void find(){
- scanf("%s",&b);
- int k=std::strlen(b);
- int x=,z,y;
- for(int i=;i<k;i++){
- z=b[i]-'a';
- while( (!e[x].next[z])&& x){
- x=e[x].fail;
- }x=e[x].next[z]; y=x;
- while(y&&e[y].count){
- ans+=e[y].count;
- e[y].count=;
- y=e[y].fail;
- }
- }
- }
- int main(){
- int T;scanf("%d",&T);
- while(T-->){
- fir();
- scanf("%d",&n);
- for(int i=;i<=n;i++){
- scanf("%s",&a);
- init(,std::strlen(a)-,);
- }
- doit();
- find();
- printf("%d\n",ans);
- }
- return ;
- }
更新:
整理模板的时候发现自己原来的代码有问题。这个新版本应该没问题了。
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<cmath>
- #include<queue>
- using namespace std;
- const int maxn=;
- int n,siz;
- char ch[]={};
- char str[maxn*]={};
- struct trie{
- int sig[];
- int cnt;
- int vis;
- int fail;
- }t[maxn*];int tot=;
- int q[maxn*]={};int head=,tail=;
- void fir(){
- tot=;memset(t,,sizeof(t));//memset(q,0,sizeof(q));
- }
- void init(int x,int j){
- if(j>siz-){ t[x].cnt++; return; }
- int z=ch[j]-'a';
- if(!t[x].sig[z])t[x].sig[z]=++tot;
- init(t[x].sig[z],j+);
- }
- void build_fail(){
- int head=,tail=,x,y,f;q[]=;
- while(head<=tail){
- x=q[head++];
- for(int i=;i<;i++)
- if(t[x].sig[i]){
- y=t[x].sig[i];
- if(x){
- f=t[x].fail;
- while((!t[f].sig[i])&&f)
- f=t[f].fail;
- t[y].fail=t[f].sig[i];
- }q[++tail]=y;
- }
- }
- }
- int get_num(){//字符串中包含的单词数量,重复的不记录,
- //如果单词x在字符串中出现一次而在单词表中有两个则ans+2
- //在字符串中出现两次而单词表中有一个则ans+1
- int ans=,x=,y,z;
- for(int i=;i<siz;i++){
- z=str[i]-'a';
- while((!t[x].sig[z])&&x)
- x=t[x].fail;
- x=t[x].sig[z];y=x;
- while(y&&(!t[y].vis)){//保证了每个结尾只访问一次
- ans+=t[y].cnt;
- t[y].vis=;t[y].cnt=;
- y=t[y].fail;
- }
- }
- return ans;
- }
- int main(){
- int T;scanf("%d",&T);
- while(T-->){
- fir();
- scanf("%d",&n);
- for(int i=;i<=n;i++){scanf("%s",ch);siz=strlen(ch);init(,);}
- build_fail();
- scanf("%s",str);siz=strlen(str);
- printf("%d\n",get_num());
- }
- return ;
- }
new
POJ2222 Keywords Search AC自动机模板的更多相关文章
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
随机推荐
- MQTT协议及推送服务
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...
- 用体渲染的方法在Unity中渲染云(18/4/4更新)
github: https://github.com/yangrc1234/VolumeCloud 更新的内容在底部 最近在知乎上看到一篇文章讲云层的渲染(https://zhuanlan.zhihu ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(4)
第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...
- HDU 1172 猜数字 (模拟)
题目链接 Problem Description 猜数字游戏是gameboy最喜欢的游戏之一.游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么.每猜一个数,计算机都会告诉玩家猜 ...
- C++ STL标准入门
C++:STL标准入门汇总 第一部分:(参考百度百科) 一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexand ...
- CentOS 6.6下目录结构及其主要作用
今天我们总结一下CentOS 6.6的linux的目录结构,一个系统的目录众多,这里我们主要认识一下,根目录下的主要目录,首先我们可以通过tree命令查看一次根目录下一层目录都有什么目录, 补充:不能 ...
- 设计模式之笔记--外观模式(Facade)
外观模式(Facade) 定义 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 类图 描述 Facade:外观类,外观 ...
- 002利用zabbix监控某个目录大小
近期,因为JMS的消息堆积导致ApacheMQ频率故障(消息没有被消费掉,导致其数据库达到1.2G,JMS此时直接挂掉),很是郁闷!刚好自 己在研究zabbix.既然zabbix如此强大,那么它可以监 ...
- angular项目中使用angular-material2
Step 1: Install Angular Material and Angular CDK npm install --save @angular/material @angular/cdk n ...
- PHP扩展插件 imagick 、PDO_MYSQL 安装
环境准备 echo $LC_ALL echo "export LC_ALL=C" >> /etc/profile source /etc/profile yum ins ...