字符串(AC自动机):COCI 2015 round 5 divljak
题目点这里,最后一题
不用bit的话最大数据2.8s,超时,有些遗憾,代码如下:
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 using namespace std; 7 const int N=2000010; 8 int n,Q,cnt,len,x,tp,top,st[N],vis[N],tim; 9 int ch[N][26],fail[N],num[N],id[N]; 10 char s[N];queue<int>q; 11 int main(){ 12 freopen("divljak.in","r",stdin); 13 freopen("divljak.out","w",stdout); 14 ios::sync_with_stdio(false); 15 cin.tie(NULL),cout.tie(NULL); 16 cin>>n; 17 for(int t=1;t<=n;t++){ 18 cin>>s; 19 len=strlen(s);int p=0; 20 for(int i=0;i<len;i++) 21 if(ch[p][s[i]-'a']) 22 p=ch[p][s[i]-'a']; 23 else 24 p=ch[p][s[i]-'a']=++cnt; 25 id[t]=p; 26 } 27 for(int i=0;i<26;i++) 28 if(ch[0][i])q.push(ch[0][i]); 29 30 while(!q.empty()){ 31 int x=q.front();q.pop(); 32 for(int i=0;i<26;i++) 33 if(ch[x][i])fail[ch[x][i]]=ch[fail[x]][i],q.push(ch[x][i]); 34 else ch[x][i]=ch[fail[x]][i]; 35 } 36 cin>>Q; 37 while(Q--){ 38 cin>>tp; 39 if(tp==1){ 40 cin>>s;top=0; 41 len=strlen(s);int p=0;++tim; 42 for(int i=0;i<len;i++){ 43 while(p&&!ch[p][s[i]-'a'])p=fail[p];p=ch[p][s[i]-'a']; 44 int tmp=p;while(tmp&&vis[tmp]!=tim)vis[st[++top]=tmp]=tim,tmp=fail[tmp]; 45 } 46 sort(st+1,st+top+1); 47 top=unique(st+1,st+top+1)-st-1; 48 for(int i=1;i<=top;i++)num[st[i]]+=1; 49 } 50 else{ 51 cin>>x; 52 cout<<num[id[x]]<<"\n"; 53 } 54 } 55 return 0; 56 }
然后我的标解超时了?
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 using namespace std; 7 const int N=2000010; 8 int n,Q,cnt,len,x,tp,top,st[N]; 9 int ch[N][26],fail[N],num[N],pd[N]; 10 int bit[N],fa[N][22],id[N],ed[N],tot; 11 int cntE,nxt[N],to[N],fir[N],dep[N]; 12 char s[N];queue<int>q; 13 14 void addedge(int a,int b){ 15 nxt[++cntE]=fir[a]; 16 to[fir[a]=cntE]=b; 17 } 18 19 void DFS(int x,int f){ 20 id[x]=++tot;dep[x]=dep[f]+1; 21 for(int i=fir[x];i;i=nxt[i]) 22 fa[to[i]][0]=x,DFS(to[i],x); 23 ed[x]=tot; 24 } 25 26 int Lca(int x,int y){ 27 if(dep[x]<dep[y])swap(x,y); 28 int d=dep[x]-dep[y]; 29 for(int i=21;i>=0;i--) 30 if(d>>i&1)x=fa[x][i]; 31 for(int i=21;x!=y;i?i--:i) 32 if(fa[x][i]!=fa[y][i]||!i) 33 x=fa[x][i],y=fa[y][i]; 34 return x; 35 } 36 37 int Add(int x,int d){ 38 while(x<N){ 39 bit[x]+=d; 40 x+=x&(-x); 41 } 42 } 43 44 int Query(int x){ 45 int ret=0; 46 while(x){ 47 ret+=bit[x]; 48 x-=x&(-x); 49 } 50 return ret; 51 } 52 53 bool cmp(int x,int y){return id[x]<id[y];} 54 55 int main(){ 56 freopen("divljak.in","r",stdin); 57 freopen("divljak.out","w",stdout); 58 ios::sync_with_stdio(false); 59 cin.tie(NULL),cout.tie(NULL); 60 cin>>n; 61 for(int t=1;t<=n;t++){ 62 cin>>s; 63 len=strlen(s);int p=0; 64 for(int i=0;i<len;i++) 65 if(ch[p][s[i]-'a']) 66 p=ch[p][s[i]-'a']; 67 else 68 p=ch[p][s[i]-'a']=++cnt; 69 pd[t]=p; 70 } 71 for(int i=0;i<26;i++) 72 if(ch[0][i])q.push(ch[0][i]); 73 74 while(!q.empty()){ 75 int x=q.front();q.pop(); 76 for(int i=0;i<26;i++) 77 if(ch[x][i])fail[ch[x][i]]=ch[fail[x]][i],q.push(ch[x][i]); 78 else ch[x][i]=ch[fail[x]][i]; 79 } 80 for(int i=1;i<=cnt;i++) 81 addedge(fail[i],i); 82 DFS(0,0); 83 for(int k=1;k<=21;k++) 84 for(int i=1;i<=cnt;i++) 85 fa[i][k]=fa[fa[i][k-1]][k-1]; 86 cin>>Q; 87 while(Q--){ 88 cin>>tp; 89 if(tp==1){ 90 cin>>s;top=0; 91 len=strlen(s);int p=0; 92 for(int i=0;i<len;i++){ 93 while(p&&!ch[p][s[i]-'a'])p=fail[p]; 94 p=ch[p][s[i]-'a'];if(p!=0)st[++top]=p; 95 } 96 sort(st+1,st+top+1,cmp);p=0; 97 for(int i=1;i<=top;i++) 98 if(st[i]!=st[i-1]) 99 st[++p]=st[i]; 100 top=p; 101 for(int i=1;i<=top;i++) 102 Add(id[st[i]],1); 103 for(int i=2;i<=top;i++) 104 Add(id[Lca(st[i],st[i-1])],-1); 105 } 106 else{ 107 cin>>x; 108 cout<<Query(ed[pd[x]])-Query(id[pd[x]]-1)<<"\n"; 109 } 110 } 111 //cout<<1.0*clock()/CLOCKS_PER_SEC<<"\n"; 112 return 0; 113 }
只快了那么一点点 = =
转载于:https://www.cnblogs.com/TenderRun/p/5869899.html
字符串(AC自动机):COCI 2015 round 5 divljak相关推荐
- 字符串-AC自动机(详细图解)
文章目录 AC自动机 原理 模板 例题 HDU-2222Keywords Search HDU-2896病毒侵袭 HDU-3065病毒侵袭持续中 POJ-2778DNA Sequence HDU-22 ...
- 字符串匹配算法 -- AC自动机 基于Trie树的高效的敏感词过滤算法
文章目录 1. 算法背景 2. AC自动机实现原理 2.1 构建失败指针 2.2 依赖失败指针过滤敏感词 3. 复杂度及完整代码 1. 算法背景 之前介绍过单模式串匹配的高效算法:BM和KMP 以及 ...
- 【AC自动机】【字符串】【字典树】AC自动机 学习笔记
blog:www.wjyyy.top AC自动机是一种毒瘤的方便的多模式串匹配算法.基于字典树,用到了类似KMP的思维. AC自动机与KMP不同的是,AC自动机可以同时匹配多个模式串, ...
- 字符串处理 —— AC 自动机
[概述] KMP 算法用于解决长文本的单模板匹配问题,字典树用于解决单个单词(短文本)多模板匹配问题,而 AC 自动机用于解决的是长文本的多模板匹配问题,其是以 trie 树的结构为基础,结合 KMP ...
- go regexp匹配字符串_多模式字符串匹配算法ac自动机(用go语言实现)
本文主要包括三部分内容 字典树 建立ac自动机 ac自动机匹配规则 字典树 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计,排序和保存大量的字符串(但不仅限 ...
- 数据结构与算法之美笔记——基础篇(下):图、字符串匹配算法(BF 算法和 RK 算法、BM 算法和 KMP 算法 、Trie 树和 AC 自动机)
图 如何存储微博.微信等社交网络中的好友关系?图.实际上,涉及图的算法有很多,也非常复杂,比如图的搜索.最短路径.最小生成树.二分图等等.我们今天聚焦在图存储这一方面,后面会分好几节来依次讲解图相关的 ...
- 字符串算法 | AC自动机算法
1.简介 一种多模式串匹配算法, 可以快速从主串中同时找出所有包含的所有模式串. 对比KMP是单模式匹配, 虽然可以使用单模式串匹配算法逐个进行查找模式串, 但是实际场景中,若模式串的数量可能很大,并 ...
- 字符串处理【AC自动机】 - 原理 AC自动机详解
字符串处理[AC自动机] - 原理 AC自动机详解 AC自动机(Aho-Corasick automaton)在1975年产生于贝尔实验室,是著名的多模匹配算法. 学习AC自动机,要有KMP和Trie ...
- Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
D. Rorororobot 题意:n*m的矩阵,从下到上是1到n行,从左到右是1到m列,每列从下到上有连续x个单元格损毁,如果给出起点和终点坐标,并且规定每次只能向同一个方向移动k次,是否能够从起点 ...
- 字符串匹配算法(AC自动机 Aho-Corasick)
文章目录 1. 多模式串匹配 2. 经典多模式串匹配--AC自动机 2.1 AC自动机构建 2.2 在AC自动机上匹配主串 2.3 复杂度分析 3. python包 1. 多模式串匹配 前面学的BF. ...
最新文章
- 运算符及题目(2017.1.8)
- 谁是中国大学理科的王者?
- Java输出数组中最长递增子序列的代码
- Xml转换成view的原理
- 全球19家值得关注的物联网安全初创企业
- 用一个按钮做主窗口,可以吗?
- python课件_如何20小时搞定Python量化期权实战?
- android html转pdf工具,android – 使用iText库将html转换为pdf时未应用hr的内联CSS
- 动手动脑及课后实践3
- 在抖音上,你只要会装穷,会装富,会吹牛逼,会包装自己,会扯淡,那翻身就指日可待了
- C#调用windows API实现 smallpdf客户端程序进行批量压缩
- python yield和generators(生成器)
- 计算机专业怎么转行做建筑,【土木转行·续集】土建类同学转行计算机,什么时间转最合适?...
- 51单片机智能小车——超声波
- 硬盘容量和计算机容量的换算,t和g的换算(硬盘容量G跟T的换算)
- 扫码枪 - 优惠卷核销
- 【转】移动端地图技术分享
- flume采集数据易出现的bug
- 快应用开发中常见的几个版本号~~
- 实名认证需谨慎,避免陷入隐私泄露风险
热门文章
- WingPro 8 for Mac(专业Python IDE开发工具)
- 如何使用Wondershare UniConverter应用效果并调整音量
- 力扣(LeetCode)56
- inux_异常_07_ftp查看不到文件列表
- java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters invo
- 全局替换字体,开源库更方便!!! 1
- centos7搭建ELK Cluster集群日志分析平台(四):Fliebeat-简单测试
- 搭建appium的android环境
- Java中三层架构与MVC之间的显著区别
- Report Service中报 RSClientController 未定义