UVAlive 4670 Dominating Patterns

题目:

 

 
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).

What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.

It is your job to find the dominating pattern(s) and their appearing times.

Input

The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N, 1N150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to 106.

At the end of the input file, number `0' indicates the end of input file.

Output

For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.

Sample Input

2
aba
bab
ababababac
6
beta
alpha
haha
delta
dede
tata
dedeltalphahahahototatalpha
0

Sample Output

4
aba
2
alpha
haha

思路:

 题目给出一个文本串多个模板串,要求出现最多的模板串。这恰好可以用AC自动机解决,只不过需要将print修改为cnt[val]++ 统计标号为val的模板串出现的次数。

原理:在文本串不同位置出现的模板都可以通过自动机匹配找到。

注意:为什么模板要开始从1标号? : 因为调用了insert(word[i],i)语句,如果给模板标号0的话相当于舍弃了这个模板串(val==0代表非单词结点),因此调用AhoCorasickaotomata的时候一定要注意不能把单词结点的val设为0。

代码:

  这里给出三份AC代码:

  无去重

 #include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<string>
using namespace std; const int maxl = + ;
const int maxw = + ;
const int maxwl = + ;
const int sigma_size = ; struct AhoCorasickaotomata{
int ch[maxl][sigma_size];
int val[maxl];
int cnt[maxw]; //计数
int f[maxl];
int last[maxl];
int sz; void clear(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
}
int ID(char c) { return c-'a'; } void insert(char* s,int v){
int u= , n=strlen(s);
for(int i=;i<n;i++){
int c=ID(s[i]);
if(!ch[u][c]) { //if ! 初始化结点
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]= sz++;
}
u=ch[u][c];
}
val[u]=v;
} void print(int j){
if(j){ //递归结尾
cnt[val[j]] ++;
print(last[j]);
}
}
void find(char* s){
int n=strlen(s);
int j=;
for(int i=;i<n;i++){
int c=ID(s[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 getFail() {
queue<int> q;
f[]=;
for(int i=;i<sigma_size;i++){ //以0结点拓展入队
int u=ch[][i];
if(u) { //u存在
q.push(u); f[u]=; last[u]=;
}
}
//按照BFS熟悉构造失配 f & last
while(!q.empty()){
int r=q.front(); q.pop();
for(int i=;i<sigma_size;i++){
int u=ch[r][i];
if(!u) continue; //本字符不存在
q.push(u);
int v=f[r];
while(v && !ch[v][i]) v=f[v]; //与该字符匹配
v=ch[v][i]; //相同字符的序号
f[u]=v;
last[u] = val[v]? v : last[v];
//递推 last
//保证作为短后缀的字串可以匹配
}
}
}
}; AhoCorasickaotomata ac;
char T[maxl]; int main(){
int n;
while(scanf("%d",&n)== && n){
char word[maxw][maxwl];
ac.clear(); //operation 1 //init
int x=n;
for(int i=;i<=n;i++){ //i 从 1 开始到 n
scanf("%s",word[i]);
ac.insert(word[i],i);
}
ac.getFail(); //operation 2
scanf("%s",T);
int L=strlen(T);
ac.find(T); //operation 3
int best = -;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++)
if(ac.cnt[i] == best) printf("%s\n",word[i]);
}
return ;
}

时间:46 ms

+map处理 

我的代码: 

 #include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<string>
using namespace std; const int maxl = + ;
const int maxw = + ;
const int maxwl = + ;
const int sigma_size = ; struct AhoCorasickaotomata{
int ch[maxl][sigma_size];
int val[maxl];
int cnt[maxw]; //计数
int f[maxl];
int last[maxl];
int sz;
map<string,int> ms; //对string打标记 避免重复 void clear(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
ms.clear();
}
int ID(char c) { return c-'a'; } void insert(char* s,int v){
int u= , n=strlen(s);
for(int i=;i<n;i++){
int c=ID(s[i]);
if(!ch[u][c]) { //if ! 初始化结点
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]= sz++;
}
u=ch[u][c];
}
val[u]=v;
} void print(int j){
if(j){ //递归结尾
cnt[val[j]] ++;
print(last[j]);
}
}
void find(char* s){
int n=strlen(s);
int j=;
for(int i=;i<n;i++){
int c=ID(s[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 getFail() {
queue<int> q;
f[]=;
for(int i=;i<sigma_size;i++){ //以0结点拓展入队
int u=ch[][i];
if(u) { //u存在
q.push(u); f[u]=; last[u]=;
}
}
//按照BFS熟悉构造失配 f & last
while(!q.empty()){
int r=q.front(); q.pop();
for(int i=;i<sigma_size;i++){
int u=ch[r][i];
if(!u) continue; //本字符不存在
q.push(u);
int v=f[r];
while(v && !ch[v][i]) v=f[v]; //与该字符匹配
v=ch[v][i]; //相同字符的序号
f[u]=v;
last[u] = val[v]? v : last[v];
//递推 last
//保证作为短后缀的字串可以匹配
}
}
}
}; AhoCorasickaotomata ac;
char T[maxl]; int main(){
int n;
while(scanf("%d",&n)== && n){
char word[maxw][maxwl];
ac.clear(); //operation 1 //init
int x=n;
for(int i=;i<=n;i++){ //i 从 1 开始到 n
scanf("%s",word[i]);
if(!ac.ms.count(word[i])){
ac.insert(word[i],i);
ac.ms[string(word[i])] =i; //string(char[])=>string
}
else x--; //改变长度
}
n=x; //n为去重之后的长
ac.getFail(); //operation 2
scanf("%s",T);
int L=strlen(T);
ac.find(T); //operation 3
int best = -;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++)
if(ac.cnt[i] == best) printf("%s\n",word[i]);
}
return ;
}

Code 1:我的代码

时间:49 ms

作者代码:

 // LA4670 Dominating Patterns
// Rujia Liu
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std; const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = + ; map<string,int> ms; struct AhoCorasickAutomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[MAXS];
int sz; void init() {
sz = ;
memset(ch[], , sizeof(ch[]));
memset(cnt, , sizeof(cnt));
ms.clear();
} // 字符c的编号
int idx(char c) {
return c-'a';
} // 插入字符串 v必须非0
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;
} // 递归打印以结点j结尾的所有字符串
void print(int j) {
if(j) {
cnt[val[j]]++;
print(last[j]);
}
} // 在T中找模板
int 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]); // 找到了
}
} // 计算fail函数
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] = ; }
}
// 按BFS顺序计算fail
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]];
}
}
} }; AhoCorasickAutomata ac;
char text[], P[][];
int n, T; int main() {
while(scanf("%d", &n) == && n) {
ac.init();
for(int i = ; i <= n; i++) {
scanf("%s", P[i]);
ac.insert(P[i], i);
}
ac.getFail();
scanf("%s", text);
ac.find(text);
int best = -;
for(int i = ; i <= n; i++)
if(ac.cnt[i] > best) best = ac.cnt[i];
printf("%d\n", best);
for(int i = ; i <= n; i++)
if(ac.cnt[ms[string(P[i])]] == best) printf("%s\n", P[i]);
}
return ;
}

Code 2:作者代码

时间:42 ms

由此可见:

因为只需要返回字串而与序号无关即使前一个模板会被后一个相同模板覆盖,但不添加map标记处理相重是可以的,因为val插入时被修改所以被覆盖的单词不会被处理cnt==0 , 而最后的一个相同的串会被操作得到正确值,因此统计时依然可以返回正确值。

   而且即使添加了map时间也不过是提高了4ms,因此并非作者在书中所言“容易忽略”而“多此一举”。

    

   可是如果出现重复模板特别多的输入的话 预判是否相同进而选择添加是可以的,但作者的map处理好像也不能加速这种情况。

   

   

【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns的更多相关文章

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

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

  2. uvalive 4670 Dominating Patterns

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

  3. UVALive - 4670 Dominating Patterns AC 自动机

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

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

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

  5. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  6. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  7. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  8. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

  9. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

随机推荐

  1. CrackMe_001

    本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注册机的东西. 其中,文章中按照如下逻辑编排(解决如 ...

  2. setjump 和 longjump

    goto语句可以用于同一个函数内异常处理,不幸的是,goto是本地的,它只能跳到所在函数内部的标号上.为了解决这个限制,C函数库提供了setjmp()和longjmp()函数,它们分别承担非局部标号和 ...

  3. 【HTTP】Fiddler(一) - Fiddler简介和使用

    1.为什么是Fiddler? 抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: a.Firebug虽然可以抓包 ...

  4. linux 下安装redis以及php Redis扩展

    [php] view plaincopy在CODE上查看代码片派生到我的代码片 linux 下安装redis以及php Redis扩展 环境配置: centos6. nginx/ php/ mysql ...

  5. c缺陷与陷阱笔记-第一章 词法陷阱

    1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...

  6. 【零基础学习iOS开发】【02-C语言】10-函数

    前面已经讲完了C语言中的基本语句和基本运算了,这讲呢,介绍C语言中的重头戏---函数.其实函数这个概念,在大部分高级语言中都是非常重要的,我也已经在<第一个C语言程序>一讲中对函数作了一个 ...

  7. C++遍历目录,并把目录里超过7天的文件删除(跨平台windows&linux)

    C++遍历目录,并把目录里超过7天的文件删除,适用于项目里删除过期的日志,或者视频文件. 在windows和linux下测试通过. windows测试结果: linux测试结果: 源码: #inclu ...

  8. DAO是什么技术

    DAO是Data Access Object数据访问接口,数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. 在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应 ...

  9. binary 和 varbinary

    固定长度或可变长度的 Binary 数据类型. binary [ ( n ) ] 长度为 n 字节的固定长度二进制数据,其中 n 是从 1 到 8,000 的值.存储大小为 n 字节. varbina ...

  10. Foreman--Puppet类导入

    一.Foreman环境: foreman建好后,系统默认创建了3个环境:production,development,common, 1. production: 在puppet.conf里已经定义其 ...