AC自动机的裸题。学了kmp和Trie以后不难看懂。

有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last。没有试过把失配边直接当成普通边(一开始还是先这样写吧)。

#include<bits/stdc++.h>
using namespace std; const int maxlen = 1e6+, maxnds = *+, sigma_size = , maxsubs = ;
char str[maxlen]; #define idx(x) x-'a';
int last[maxnds];//后缀连接 为0表示空
int ch[maxnds][sigma_size];
int val[maxnds];
int nds;
char subs[maxsubs][];
int cnt[maxsubs+];
int f[maxnds]; void init() {
memset(ch,,sizeof(ch)); last[] = ; val[] = ; nds = ;
memset(cnt,,sizeof(cnt));
} void cal(int j)
{
while(j){
cnt[val[j]]++;
j = last[j];
}
} void Find(char *t)
{
for(int i = , j = ; t[i] ; i++){
int c = idx(t[i]);
while(j && !ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) cal(j);
else if(last[j]) cal(last[j]);
}
} int getF()
{
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u) { f[u] = ; q.push(u); last[u] = ; }
} while(q.size()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u) continue;
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} void add(char *s,int str_id)
{
int u = ;
for(int i = ; s[i]; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[nds],,sizeof(ch[nds]));
val[nds] = ;
ch[u][c] = nds++;
}
u = ch[u][c];
}
val[u] = str_id;
} int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d\n",&n),n){
init();
for(int i = ; i <= n; i++){
scanf("%s",subs[i]);
add(subs[i],i);
}
getF();
scanf("%s",str);
Find(str);
int best = *max_element(cnt+,cnt++n);
printf("%d\n",best);
for(int i = ; i <= n; i++){
if(cnt[i] == best) puts(subs[i]);
}
}
return ;
}

UVALive 4670 Dominating Patterns (AC自动机)的更多相关文章

  1. UVALive 4670 Dominating Patterns --AC自动机第一题

    题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...

  2. UVALive - 4670 Dominating Patterns AC 自动机

    input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...

  3. LA 4670 Dominating Patterns (AC自动机)

    题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...

  4. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

  5. uvalive 4670 Dominating Patterns

    在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...

  6. UVa1449 - Dominating Patterns(AC自动机)

    题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...

  7. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  8. LA4670 Dominating Patterns AC自动机模板

    Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...

  9. UVa Live 4670 Dominating Patterns - Aho-Corasick自动机

    题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...

随机推荐

  1. spring-boot-starter-data-redis学习笔记01

    1.Redis在Unbuntu14开启, 进入安装的src目录: 1.修改redis.conf,因为redis默认是受保护模式. protected-mode yes   (改为no) bind 12 ...

  2. MVC动态生成的表单:表单元素比较多 你就这样写

    MVC动态生成的表单:表单元素比较多 你就这样写: public ActionResult ShoudaanActionResult(FormCollection from,T_UserM user) ...

  3. unreal4特性介绍

    原文地址:   https://www.unrealengine.com/products/unreal-engine-4     unreal enginer介绍 我的UE4学习(一) 你曾想过用连 ...

  4. 洛谷P3704 [SDOI2017]数字表格(莫比乌斯反演)

    传送门 yyb大佬太强啦…… 感觉还是有一点地方没有搞懂orz //minamoto #include<cstdio> #include<iostream> #include& ...

  5. LInux temp 目录太小不够用问题

    2013-10-18 11:01 可以自己建一个临时 目录看看 dev /#su - root #mkdir /opt/tmp #chown root /opt/tmp/ #chgrp root /o ...

  6. git clone 指定新建本地库位置

  7. STP-9-处理RSTP中的拓扑变化

    STP能识别四种不同的拓扑变化事件, 而RSTP只有当非边界端口从非转发状态变为转发状态才认为发生了拓扑变化事件 因为刚变为转发状态的端口可以为一些mac地址提供比之前更好的路径,CAM表需要更新.失 ...

  8. Linux新硬盘、分区、格式化、自动挂载

    1.给硬盘分区 fdisk /dev/sdaCommand (m for help): nCommand actione extendedp primary partition (1-4)输入:ePa ...

  9. Codeforces 1163D(kmp、dp)

    要点 \(dp[i][j][k]\)表示主串已经到第\(i\)位时,\(s\)匹配在\(j\)位.\(t\)匹配在\(k\)位的最大得分 本来就要试填一层循环,如果转移也写在循环里的化复杂度承受不了, ...

  10. Ubuntu上源码安装golang并设置开发环境

    安装go #wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz #tar -xzf go1.10.3.linux-amd64.tar.g ...