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

分析:

  • 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. 【3dsMax安装失败,如何卸载、安装3dMax 2012?】

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  2. zookeeper简单命令

    bin/zkCli.sh -server ls / create /zk_test my_data get /zk_test set /zk_test admln delete /zk_test ad ...

  3. import java.util.Collections类

    Collections类提供了一些操作集合的方法  下面介绍几个方法 1.将集合变为线程安全的 三个方法分别对应了ArrayList,HashMap,HashSet: Collections.sync ...

  4. The Definitive C++ Book Guide and List--reference

    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Reference Style - All ...

  5. ContextCapture水面约束(水面破洞修复)

      [问题描述] 对于水面而言,由于特征点较少,软件在计算时很难匹配正确,导致输出模型的水面通常是支离破碎的.软件针对这种情况提供了一个约束工具,用户手动的为水面添加平面约束后,输出的水面模型就会非常 ...

  6. HDU 5635 ——LCP Array ——————【想法题】

    LCP Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  7. ubuntu遇到了 dpkg was interrupted, you must manually run 'dpkg..的问题

    dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. E: _cache- ...

  8. 深入理解JavaScript系列(8):S.O.L.I.D五大原则之里氏替换原则LSP

    前言 本章我们要讲解的是S.O.L.I.D五大原则JavaScript语言实现的第3篇,里氏替换原则LSP(The Liskov Substitution Principle ). 英文原文:http ...

  9. Distinct 去掉重复 order by

    语法: select distinct user from book select * from sdudant order by sex asc,sNo 从表sdudant查找已性别升序排序,性别相 ...

  10. [转]谷歌Chrome浏览器开发者工具教程—基础功能篇

    来源:http://www.xiazaiba.com/jiaocheng/5557.html Chrome(F12开发者工具)是非常实用的开发辅助工具,对于前端开发者简直就是神器,但苦于开发者工具是英 ...