洛谷-P3796-AC自动机加强版
链接:
https://www.luogu.org/problem/P3796
题意:
有NN个由小写字母组成的模式串以及一个文本串TT。每个模式串可能会在文本串中出现多次。你需要找出哪些模式串在文本串TT中出现的次数最多。
思路:
字典树的每个结束节点记录对应的模板串标号, 匹配时记录次数.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 2e6+10;
struct Trie
{
int Next[26];
int End;
int Fail;
void Init()
{
memset(Next, 0, sizeof(Next));
End = Fail = 0;
}
}trie[MAXN];
int cnt, n, pos, maxt;
int Ans[MAXN];
char s[MAXN], Pt[200][100];
void Insert(char *t, int id)
{
int len = strlen(t);
int p = 0;
for (int i = 0;i < len;i++)
{
if (trie[p].Next[t[i]-'a'] == 0)
{
trie[p].Next[t[i]-'a'] = ++cnt;
trie[cnt].Init();
}
p = trie[p].Next[t[i]-'a'];
}
trie[p].End = id;
}
void BuildAC()
{
queue<int> que;
for (int i = 0;i < 26;i++)
{
if (trie[0].Next[i] != 0)
que.push(trie[0].Next[i]);
}
while (!que.empty())
{
int u = que.front();
que.pop();
for (int i = 0;i < 26;i++)
{
if (trie[u].Next[i])
{
trie[trie[u].Next[i]].Fail = trie[trie[u].Fail].Next[i];
que.push(trie[u].Next[i]);
}
else
trie[u].Next[i] = trie[trie[u].Fail].Next[i];
//压缩路径, 没有的点直接指向别的节点, 减少向上找的时间
}
}
}
void Query(char *qs)
{
int len = strlen(qs);
int p = 0;
for (int i = 0;i < len;i++)
{
p = trie[p].Next[qs[i]-'a'];
for (int j = p;j != 0 && trie[j].End != -1;j = trie[j].Fail)
{
//将所有能出现的匹配都跑一遍, 同时处理防止多跑.
if (trie[j].End != 0)
{
Ans[trie[j].End]++;
maxt = max(maxt, Ans[trie[j].End]);
}
}
}
}
int main()
{
while (~scanf("%d", &n) && n)
{
memset(Ans, 0, sizeof(Ans));
cnt = pos = maxt = 0;
trie[0].Init();
for (int i = 1;i <= n;i++)
{
scanf("%s", Pt[i]);
Insert(Pt[i], i);
}
scanf("%s", s);
BuildAC();
Query(s);
if (maxt <= 0)
puts("0");
else
{
printf("%d\n", maxt);
for (int i = 1;i <= n;i++)
{
if (Ans[i] == maxt)
puts(Pt[i]);
}
}
}
return 0;
}
洛谷-P3796-AC自动机加强版的更多相关文章
- [模板][P3796]AC自动机(加强版)
Description: 输出有哪些模式串在文本串中出现次数最多,这个次数是多少 Hint: 多组数据,$ len_{文本串}<=10^6,\sum len_{模式串} <= 70*150 ...
- 洛谷 P3796 【模板】AC自动机(加强版)(AC自动机)
题目链接:https://www.luogu.com.cn/problem/P3796 AC自动机:复杂度$O( (N+M)\times L )$,N为模式串个数,L为平均长度,M为文章长度. ins ...
- 洛谷P3796 【模板】AC自动机(加强版)(AC自动机)
洛谷题目传送门 先膜一发yyb巨佬 orz 想学ac自动机的话,推荐一下yyb巨佬的博客,本蒟蒻也是从那里开始学的. 思路分析 裸的AC自动机,这里就不讲了.主要是这题太卡时了,尽管时限放的很大了.. ...
- 洛谷P3796 - 【模板】AC自动机(加强版)
原题链接 Description 模板题啦~ Code //[模板]AC自动机(加强版) #include <cstdio> #include <cstring> int co ...
- 洛谷P3808 & P3796 AC自动机模板
题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...
- 洛谷P3796
题目链接 题意:有n个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.哪些模式串在文本串T中出现的次数最多. 题解:ac自动机模板加强版,开一个数组单独记录各个字符串出现 ...
- [codevs1048]石子归并&[codevs2102][洛谷P1880]石子归并加强版
codevs1048: 题目大意:有n堆石子排成一列,每次可合并相邻两堆,代价为两堆的重量之和,求把他们合并成一堆的最小代价. 解题思路:经典区间dp.设$f[i][j]$表示合并i~j的石子需要的最 ...
- 洛谷 P6031 - CF1278F Cards 加强版(推式子+递推)
洛谷题面传送门 u1s1 这个推式子其实挺套路的吧,可惜有一步没推出来看了题解 \[\begin{aligned} res&=\sum\limits_{i=0}^ni^k\dbinom{n}{ ...
- 洛谷 P3808 【模板】AC自动机(简单版)洛谷 P3796 【模板】AC自动机(加强版)
https://www.cnblogs.com/gtarcoder/p/4820560.html 每个节点的后缀指针fail指针指向: 例如he,she,his,hers的例子(见蓝书P214): 7 ...
- cjoj P1435 - 【模板题 USACO】AC自动机 && 洛谷 P3796 【模板】AC自动机(加强版)
又打了一遍AC自动稽. 海星. 好像是第一次打trie图,很久以前就听闻这个思想了.OrzYYB~ // It is made by XZZ #include<cstdio> #inclu ...
随机推荐
- 三维空间建模方法之LOD模型算法
什么是LOD LOD也称为层次细节模型,是一种实时三维计算机图形技术,最先由Clark于1976年提出,其工作原理是: 视点离物体近时,能观察到的模型细节丰富:视点远离模型时,观察到的细节逐渐模糊.系 ...
- 导出远程oracle数据库到本地
1.以管理员身份运行 Net Manager 以管理员身份运行cmd
- ARC083E. Bichrome Tree
A viable configuration of the given tree can be divided into two trees, each consists of vertices of ...
- 如何使用RedisTemplate访问Redis数据结构之Hash
Redis的Hash数据机构 Redis的散列可以让用户将多个键值对存储到一个Redis键里面. public interface HashOperations<H,HK,HV> Hash ...
- c# 金钱大写转小写工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- StoneTab标签页CAD插件 3.2.1
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- C#端口、IP正则
端口正则: string pattrn = "^[0-9]+$"; if (System.Text.RegularExpressions.Regex.IsMatch(Porttex ...
- WindowsAPI操作串口
#include <windows.h> #include <stdio.h> int main() { //1.打开串口 HANDLE hCom; hCom = Create ...
- 转载:Web安全 之 X-Frame-Options响应头配置
转自:https://blog.csdn.net/u013310119/article/details/81064943 项目检测时,安全报告中存在 “X-Frame-Options” 响应头缺失问题 ...
- mybatis+Oracle 批量插入数据,有数据做更新操作
<!-- 批量添加 --> <insert id="batchAdd" parameterType="java.util.List"& ...