UVa1449 - Dominating Patterns(AC自动机)
题目大意
给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多
题解
一个文本串,多个模式串,这刚好是AC自动机处理的问题
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
using namespace std;
const int maxnode=11111;
const int sigma_size=26;
const int maxs=160;
map<string,int> ms;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size],last[maxnode];
int fail[maxnode],end[maxnode],cnt[160];
int sz;
void init()
{
memset(ch[0],0,sizeof(ch[0]));
memset(cnt,0,sizeof(cnt));
sz=1,ms.clear();
}
int idx(char c){return c-'a';}
void insert(char *s,int v)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
end[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
end[u]=v,ms[string(s)]=v;
}
void getfail()
{
queue<int>q;
fail[0]=0;
for(int c=0;c<sigma_size;c++)
{
int u=ch[0][c];
if(u){ fail[u]=0;q.push(u);last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<sigma_size;c++)
{
int u=ch[r][c];
if(!u){ch[r][c]=ch[fail[r]][c];continue;}
q.push(u);
fail[u]=ch[fail[r]][c];
last[u] = end[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void count(int j)
{
if(j)
{
cnt[end[j]]++;
count(last[j]);
}
}
void find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
j=ch[j][c];
if(end[j]) count(j);
else if(last[j]) count(last[j]);
}
}
};
AhoCorasickAutomata ac;
char text[1000005],P[160][80];
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
ac.init();
for(int i=1;i<=n;i++)
{
scanf("%s",P[i]);
ac.insert(P[i],i);
}
ac.getfail();
scanf("%s",text);
ac.find(text);
int best=-1;
for(int i=1;i<=n;i++)
if(ac.cnt[i]>best) best=ac.cnt[i];
printf("%d\n",best);
for(int i=1;i<=n;i++)
if(ac.cnt[ms[string(P[i])]]==best) printf("%s\n",P[i]);
}
return 0;
}
UVa1449 - Dominating Patterns(AC自动机)的更多相关文章
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- UVa 1449 - Dominating Patterns (AC自动机)
题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...
- LA 4670 Dominating Patterns (AC自动机)
题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...
- LA4670 Dominating Patterns AC自动机模板
Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...
- UVALive - 4670 Dominating Patterns AC 自动机
input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...
- UVA1449 Dominating Patterns
UVA1449 Dominating Patterns 题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输 ...
- UVa Live 4670 Dominating Patterns - Aho-Corasick自动机
题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...
- Dominating Patterns (AC 自动鸡模版题, 出现次数最多的子串)
传送门 题意: 给你n个模式串, 再给你一个 文本串,问模式串在文本串中出现次数最多是多少. 出现次数最多的模式串有哪些. 解: 模版题. #include <bits/stdc++.h> ...
- UVALive-4670 Dominating Patterns(AC自动机)
题目大意:找出出现次数最多的模式串. 题目分析:AC自动机裸题. 代码如下: # include<iostream> # include<cstdio> # include&l ...
随机推荐
- C++ 实现设计模式之观察者模式
1. 什么是观察者模式? 观察者模式(有时又被称为发布-订阅Subscribe>模式.模型-视图View>模式.源-收听者Listener>模式或从属者模式)是软件设计模式的一种.在 ...
- myeclipse10.0优化
一.Myeclipse10修改字体 MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...
- 【git】切换分支获取代码
Welcome to Git (version 1.9.5-preview20150319) Run 'git help git' to display the help index.Run 'git ...
- Win10 IIS以及ASP.NET 4.0配置问题日志
问题日志 升级到Win10并安装了VS2015后,原有ASP.NET 4.0项目在本机的IIS部署出现问题. 安装IIS: 在[控制面板.程序.启用或关闭Windows功能.Internet Info ...
- KVC vs KVO(内容为转载记录,整合大家的总结为我所用)
KVC即key-value coding的缩写, KVO即key-value observing的缩写 假如需要掌握Key-Value Observing机制,那么需要阅读本文应该有帮助.本文提供了K ...
- Jquer学习
1:什么是Jquery 2:jquery的例子 3:后记
- 让Eclipse使用新版本的JRE
更新到新的 Mac OS X 再打开Eclipse 编译程序会报错, Exception in thread "main" java.lang.UnsupportedClassVe ...
- Hansight
http://www.hansight.com/scenarios.html#account
- c++学习之旅-Cygwin+Eclipse ide for c++
一,cygwin下载完毕后配置系统环境片两path指向cygwin/bin 二,eclipse设置 2.1 设置工作目录的cygwin映射 cygwin/d ->d:\ 2.2设置编译 下面新建 ...
- *[topcoder]AstronomicalRecords
http://community.topcoder.com/stat?c=problem_statement&pm=12804&rd=15706 首先A和B的长度都不一定一样,里面的元 ...