AC自动机,找出现最多的子串

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

[Submit]   [Go Back]   [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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>using namespace std;const int maxn=160*80;struct WORD
{int id,cnt;char str[80];
}w[160];
int hash[maxn];
int chd[maxn][26],f[maxn],val[maxn],last[maxn],sz;bool cmp(WORD a,WORD b)
{if(a.cnt!=b.cnt){return a.cnt>b.cnt;}else{return a.id<b.id;}
}void init()
{sz=1;memset(chd[0],0,sizeof(chd[0]));memset(f,0,sizeof(f));memset(val,0,sizeof(val));memset(last,0,sizeof(last));
}void insert(char* p,int i)
{int u=0;for(;*p;p++){if(chd[u][*p-'a']==0){memset(chd[sz],0,sizeof(chd[sz]));chd[u][*p-'a']=sz++;}u=chd[u][*p-'a'];}val[u]++;hash[u]=i;
}void getFail()
{queue<int> q;f[0]=0;for(int c=0;c<26;c++){int u=chd[0][c];if(u){q.push(u);f[u]=last[u]=0;}}while(!q.empty()){int r=q.front(); q.pop();for(int c=0;c<26;c++){int u=chd[r][c];if(!u) {chd[r][c]=chd[f[r]][c];continue;}q.push(u);int v=f[r];while(v&&!chd[v][c]) v=f[v];f[u]=chd[v][c];last[u]=val[u]?f[u]:last[f[u]];}}
}void solve(int j)
{if(!j) return ;int u=hash[j];w[u].cnt++;
}int find(char* T)
{int n=strlen(T);int j=0;getFail();for(int i=0;i<n;i++){j=chd[j][T[i]-'a'];if(val[j]) solve(j);else if(last[j]) solve(last[j]);}
}int main()
{int n;char T[1100000];while(scanf("%d",&n)!=EOF&&n){init();for(int i=0;i<n;i++){scanf("%s",w[i].str);w[i].id=i;w[i].cnt=0;insert(w[i].str,i);}scanf("%s",T);find(T);sort(w,w+n,cmp);int mxx=w[0].cnt;printf("%d\n",mxx);for(int i=0;i<n;i++){if(w[i].cnt==mxx){printf("%s\n",w[i].str);}else break;}}return 0;
}

UvaLA 4670 Dominating Patterns相关推荐

  1. LA4670 Dominating Patterns[AC自动机]

    The archaeologists are going to decipher a very mysterious "language". Now, they know many ...

  2. Dominating Patterns

    题目链接:http://vjudge.net/problem/36265 #include<bits/stdc++.h> #define N 200 using namespace std ...

  3. AC自动机加强版 uva 1449 - Dominating Patterns

    AC自动机最初作用  一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过. 当然这不是AC自动机的全部作用. 本文就是一例,给出几个单词,查询在text里 ...

  4. jenkins换服务器找不到包,服务器重启后Jenkins项目部分丢失问题解决方法

    UVALive 4670 Dominating Patterns --AC自动机第一题 题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #i ...

  5. LA_4670_Dominating_Patterns_(AC自动机+map)

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

  6. [转] Leaving patterns practices

    [J.D. Meier's Blog]"Life is like skiing.  Just like skiing, the goal is not to get to the botto ...

  7. 视频中的运动特征--Learning Motion Patterns in Videos

    Learning Motion Patterns in Videos CVPR2017 Torch code: http://thoth.inrialpes.fr/research/mpnet 本文要 ...

  8. Conventions and patterns for multi-platform development

    For Developers‎ > ‎Design Documents‎ > ‎ Conventions and patterns for multi-platform developme ...

  9. 艾伟_转载:C# Design Patterns (3) - Decorator

    Decorator Pattern (装饰模式) 装饰模式可「动态」地给一个对象添加一些额外的职责,提供有别于「继承」的另一种选择.就扩展功能而言,Decorator Pattern 透过 Aggre ...

  10. WHAT THE DATA SAYS ABOUT KUBERNETES DEPLOYMENT PATTERNS

    2019独角兽企业重金招聘Python工程师标准>>> WHAT THE DATA SAYS ABOUT KUBERNETES DEPLOYMENT PATTERNS The con ...

最新文章

  1. 服务器自动post,jquery ajax $.post自动变GET的解决方式(for CI)
  2. 秒解决PHP 500的问题
  3. 打开PDF文件弹出阅读未加标签文档的解决方法
  4. php返回图片给安卓_android上传图片到PHP的过程详解
  5. Leetcode224 基本加减计算器-双栈和状态转换
  6. ARP(Address Resolution Protocol)地址解析协议初识
  7. 修改mysql 外删除用户_mysql添加用户、删除用户、授权、修改密码等
  8. 唯一约束 mysql
  9. 抢完口罩抢头盔!头盔销量激增,价格翻倍:昨天59元,今天258元
  10. Python爬虫辅助库BeautifulSoup4用法精要
  11. MapXtreme2004 连接oracle spatial的问题
  12. 不受支持的SQL类型1111
  13. linspace函数matlab_MATLAB用不同颜色绘制多条曲线
  14. Zabbix监控配置
  15. maven上传pom文件
  16. android 导航栏动画,使用Lottie动画实现底部导航栏
  17. 在Linux中禅道的安装流程
  18. iPhone, iPad, 的Safari书签和阅读列表不同步问题
  19. 【一起入门NLP】中科院自然语言处理作业三:用BiLSTM+CRF实现中文命名实体识别(TensorFlow入门)【代码+报告】
  20. Bonobo基于.NET-Git服务器

热门文章

  1. android手机什么架构图,从架构图看Android分为几层呢?
  2. 【JS】阮一峰js教程总结
  3. Unity,Sketchfab和Verge3D对比
  4. windows电脑提醒功能,定时打卡
  5. 网络摄像头RTSP流媒体协议视频平台EasyNVR如何进行延迟测试?
  6. js相对视口的高度_js获取可视区域高度
  7. 关于去除“请选择”问题
  8. c盘压缩卷压缩不了怎么办 c盘压缩卷只能压缩一半的解决方法
  9. 没有基础学习大数据难吗?
  10. matlab 函数pdf怎么用_PDF文档怎么进行批量旋转?调整页面用迅捷PDF转换器