思路

对所有模式串建AC自动机
在AC自动机上跑文本串,得出每个模式串的出现次数,输出即可

AC自动机中的fail指针的意思是指向当前节点的最长的在Trie中存在和该最长后缀相等的前缀的节点

我的AC自动机写法以0为根,0为中止条件

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int Trie[12000][26],Nodecnt,root,fail[12000],isend[12000],cnt[12000],n,ans;
char s[1001000],t[12000][100];
void insert(char *s,int len,int inq){int o=root;for(int i=0;i<len;i++){if(!Trie[o][s[i]-'a'])Trie[o][s[i]-'a']=++Nodecnt;o=Trie[o][s[i]-'a'];}isend[o]=inq;
}
queue<int> q;
void build_AC(void){for(int i=0;i<26;i++)if(Trie[root][i]){fail[Trie[root][i]]=root;q.push(Trie[root][i]);}while(!q.empty()){int x=q.front();q.pop();for(int i=0;i<26;i++){if(Trie[x][i]){fail[Trie[x][i]]=Trie[fail[x]][i];q.push(Trie[x][i]);}elseTrie[x][i]=Trie[fail[x]][i];}}
}
void query(char *s,int len){int o=root;for(int i=0;i<len;i++){o=Trie[o][s[i]-'a'];int p=o;while(p){if(isend[p])cnt[isend[p]]++;p=fail[p];}}
}
void init(void){Nodecnt=0;root=0;memset(Trie,0,sizeof(Trie));memset(fail,0,sizeof(fail));memset(isend,0,sizeof(isend));memset(cnt,0,sizeof(cnt));ans=0;
}
int main(){while(scanf("%d",&n)==1&&n){init();for(int i=1;i<=n;i++){scanf("%s",t[i]);insert(t[i],strlen(t[i]),i);}build_AC();scanf("%s",s);query(s,strlen(s));for(int i=1;i<=n;i++)ans=max(ans,cnt[i]);printf("%d\n",ans);for(int i=1;i<=n;i++)if(cnt[i]==ans)printf("%s\n",t[i]); }return 0;
}

转载于:https://www.cnblogs.com/dreagonm/p/10692179.html

UVA1449 Dominating Patterns相关推荐

  1. LA4670 Dominating Patterns[AC自动机]

    The archaeologists are going to decipher a very mysterious "language". Now, they know many ...

  2. Dominating Patterns

    题目链接:http://vjudge.net/problem/36265 #include<bits/stdc++.h> #define N 200 using namespace std ...

  3. AC自动机加强版 uva 1449 - Dominating Patterns

    AC自动机最初作用  一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过. 当然这不是AC自动机的全部作用. 本文就是一例,给出几个单词,查询在text里 ...

  4. jenkins换服务器找不到包,服务器重启后Jenkins项目部分丢失问题解决方法

    UVALive 4670 Dominating Patterns --AC自动机第一题 题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #i ...

  5. LA_4670_Dominating_Patterns_(AC自动机+map)

    描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. [转] Leaving patterns practices

    [J.D. Meier's Blog]"Life is like skiing.  Just like skiing, the goal is not to get to the botto ...

  7. 视频中的运动特征--Learning Motion Patterns in Videos

    Learning Motion Patterns in Videos CVPR2017 Torch code: http://thoth.inrialpes.fr/research/mpnet 本文要 ...

  8. Conventions and patterns for multi-platform development

    For Developers‎ > ‎Design Documents‎ > ‎ Conventions and patterns for multi-platform developme ...

  9. 艾伟_转载:C# Design Patterns (3) - Decorator

    Decorator Pattern (装饰模式) 装饰模式可「动态」地给一个对象添加一些额外的职责,提供有别于「继承」的另一种选择.就扩展功能而言,Decorator Pattern 透过 Aggre ...

  10. WHAT THE DATA SAYS ABOUT KUBERNETES DEPLOYMENT PATTERNS

    2019独角兽企业重金招聘Python工程师标准>>> WHAT THE DATA SAYS ABOUT KUBERNETES DEPLOYMENT PATTERNS The con ...

最新文章

  1. “云办公”:重新定义工作手机
  2. python好用-6个炫酷又好用的 Python 工具,个个都很奔放呀
  3. ES6之Module 的加载实现(1)
  4. Python中的字符串与字符编码:编码和转换问题
  5. oracle如何不让表自动建分区,怎么自动创建表空间和表分区
  6. ZPCategory
  7. ---perl 模块安装方法
  8. Java学习笔记(完结)
  9. ms17010批量扫描备忘
  10. 任小龙 mysql笔记_小码哥-任小龙Java笔记-面向对象的全部笔记.pdf
  11. Python数据分析之数据聚合与分组运算
  12. win7下修改php.ini的配置路径
  13. 问题 C: 小写转大写
  14. 【java】根据当前时区获取时间
  15. 结合Delphi和Python的优势:使用Delphi VCL组件快速构建超现代的Python本机Windows GUI桌面酷炫用户界面应用
  16. python2升级python3语法错误总结
  17. 查看是否安装了VC++ 2015-2019 Redistributeable
  18. PotPlayer 播放器美化设置
  19. 传统算法与神经网络算法,常见的神经网络算法有
  20. 从零学习PyTorch(5)----整个天空都是灰蒙蒙的

热门文章

  1. PVR图像文件格式初探
  2. android项目银联支付
  3. View inflate 原理
  4. 服务架构演变~超详细
  5. 防干扰继电器控制电路
  6. wordpress主题 Cosy主题3.X下载
  7. java pem 签名_如何在Java中验证PEM格式证书
  8. 华夏文化-让你心醉又心碎的陶笛--故乡的原风景
  9. 蓝桥杯-三角形 已知三点求三角形周长、面积、外心、重心
  10. 时间戳——微信保存视频时发现的问题