题意:给一个字典,看这个字典中匹配最多次数的是哪个单词(可以有多个)。

分析:

  • AC自动机就是用来解决多模式匹配问题的工具。
  • 模板用的lrj的,相比HDU 2222,动态开辟字典树。用last数组统计字典。
  • 统计每一个单词匹配的次数cnt[],下标唯一对应val,最后遍历一遍cnt。
#include <bits/stdc++.h>

using namespace std;

const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = +; map<string,int> ms; struct Aho {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE];
int last[MAXNODE];
int cnt[MAXS];
int val[MAXNODE];
int sz; void init() {
sz = ;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
ms.clear();
} int idx(char c) {
return c - 'a';
} void insert(char *s,int v) {
int u = ,n = strlen(s);
for(int i=; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz],,sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
ms[string(s)] = v;
} void getFail() {
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.empty()) { //失配是一个宽搜的过程
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]]; //last 方便统计
}
} } void find(char* T) {
int n = strlen(T);
int j = ; for(int i=; i < n; i++) {
int c = idx(T[i]);
while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j])
print(last[j]);
}
} void print(int j) {
if(j) {
cnt[val[j]]++;
print(last[j]);
}
} }aho; int n,T; char text[],P[][]; int main(int argc, char const *argv[])
{
while(scanf("%d",&n),n) {
aho.init(); for(int i=; i <= n; i++) {
scanf("%s",P[i]);
aho.insert(P[i],i);
} aho.getFail(); scanf("%s",text);
aho.find(text); int ans = -;
for(int i=; i <= n; i++)
if(aho.cnt[i]>ans) ans = aho.cnt[i]; printf("%d\n",ans); for(int i=; i <= n; i++)
if(aho.cnt[i]==ans)
printf("%s\n",P[i]);
}
return ;
}

LA 4670 AC自动机的更多相关文章

  1. LA 4670 (AC自动机 模板题) Dominating Patterns

    AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...

  2. La 4670 AC自动机(模版)

    #include<iostream> #include<cstring> #include<queue> #include<cstdio> #inclu ...

  3. UVALive 4670 AC自动机

    第二道AC自动机的题目了,之前参考的是网上一个博客算法,不怎么好,难写而且占空间 后来参照大白书做的这题,代码简洁多了 #include <iostream> #include <c ...

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

    题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多. 析:一匹配多,很明显是AC自动机.只需要对原来的进行修改一下,就可以得到这个题的答案, 计算过程中,要更新次数,并且要映射字符串.如 ...

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

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

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

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

  7. UVALive 4670 Dominating Patterns (AC自动机)

    AC自动机的裸题.学了kmp和Trie以后不难看懂. 有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last.没有试过把失配边直接当成普通边(一开始还是先这样写吧). #inc ...

  8. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. LA_4670_Dominating_Patterns_(AC自动机+map)

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

随机推荐

  1. 网络编程api bind函数细节 select 细节

    struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ ...

  2. ansible 入门学习(一)

    一,ansible 目录结构 (来自于ansible权威指南) 二,ansible.cfg 配置项说明 /etc/ansible/ansible.cfg --> ———————————————— ...

  3. AttackEnemy人物攻击判断

    AttackEnemy人物攻击判断 /// <param name="attackArea">攻击范围</param> /// <param name ...

  4. angular 兼容IE浏览器

    安装classlist.babel-polyfill: npm install --save classlist.js npm install --save babel-polyfill 修改 src ...

  5. centos 6.5搭建LNMP环境

    1:查看环境: 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 1 [r ...

  6. CF 305C ——Ivan and Powers of Two——————【数学】

    Ivan and Powers of Two time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. 20个最受欢迎的Linux命令(转)

    本文根据 commandlinefu 网站的历史排名,筛选出了前 20 个得票最高的 Linux 命令.看看你都能熟练使用了吗? 1.以 root 帐户执行上一条命令 sudo !! 2.利用 Pyt ...

  8. 自己用jquery+css+div写的一个弹窗

    弹窗支持两种模式,一种是普通信息提示框,调用方法:popup.msgPopup(msg); 另一种是可以加载页面的弹窗,调用方法:popup.pagePopup(url); 效果图: css代码 ;; ...

  9. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  10. Android DataBinding实现地址三联动

    这篇文章主要是写关于Android实现地址三联动的功能,现在附上demo地址:https://github.com/qiuyueL/NewAddressDemo,里面会有详细的注释,以及控件的使用,其 ...