Dominating Patterns
Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
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 patternsN, 1N
150. 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 to106.
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自动机;
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <cstring>
- #include <string>
- #include <set>
- #include <map>
- #include <unordered_map>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <list>
- #define rep(i,m,n) for(i=m;i<=n;i++)
- #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
- #define mod 1000000007
- #define inf 0x3f3f3f3f
- #define vi vector<int>
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define ll long long
- #define pi acos(-1.0)
- #define pii pair<int,int>
- #define Lson L, mid, ls[rt]
- #define Rson mid+1, R, rs[rt]
- #define sys system("pause")
- #define freopen freopen("in.txt","r",stdin)
- const int maxn=1e5+;
- using namespace std;
- ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
- ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
- inline ll read()
- {
- ll x=;int f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int n,m,k,t,ma;
- char a[],b[][];
- struct node
- {
- int ch[maxn][],f[maxn],val[maxn],last[maxn],cnt[maxn],sz;
- void init()
- {
- sz=;
- memset(ch[],,sizeof(ch[]));
- memset(cnt,,sizeof(cnt));
- }
- int idx(char x)
- {
- return x-'a';
- }
- void insert(char *p,int v)
- {
- int pos=;
- for(int i=;p[i];i++)
- {
- int x=idx(p[i]);
- if(!ch[pos][x])
- {
- ch[pos][x]=++sz;
- memset(ch[sz],,sizeof(ch[sz]));
- val[sz]=;
- }
- pos=ch[pos][x];
- }
- val[pos]=v;
- }
- void out(int x)
- {
- if(x)
- {
- cnt[val[x]]++;
- out(last[x]);
- }
- }
- void find(char *p)
- {
- int pos=;
- for(int i=;p[i];i++)
- {
- int x=idx(p[i]);
- pos=ch[pos][x];
- if(val[pos])out(pos);
- else if(last[pos])out(last[pos]);
- }
- }
- void fail()
- {
- queue<int>p;
- f[]=;
- for(int i=;i<;i++)
- {
- int x=ch[][i];
- if(x)f[x]=,p.push(x),last[x]=;
- }
- while(!p.empty())
- {
- int x=p.front();
- p.pop();
- for(int i=;i<;i++)
- {
- int y=ch[x][i];
- if(!y)
- {
- ch[x][i]=ch[f[x]][i];
- continue;
- }
- p.push(y);
- int v=f[x];
- while(v&&!ch[v][i])v=f[v];
- f[y]=ch[v][i];
- last[y]=val[f[y]]?f[y]:last[f[y]];
- }
- }
- }
- }ac;
- int main()
- {
- int i,j;
- while(~scanf("%d",&n)&&n)
- {
- ac.init();
- rep(i,,n)
- {
- scanf("%s",b[i]);
- ac.insert(b[i],i);
- }
- ac.fail();
- scanf("%s",a);
- ac.find(a);
- ma=;
- rep(i,,n)ma=max(ma,ac.cnt[i]);
- printf("%d\n",ma);
- rep(i,,n)if(ma==ac.cnt[i])printf("%s\n",b[i]);
- }
- //system("Pause");
- return ;
- }
Dominating Patterns的更多相关文章
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns 题目: Dominating Patterns Time Limit: 3000MS Memory Limit: Unkn ...
- UVA1449 Dominating Patterns
UVA1449 Dominating Patterns 题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输 ...
- LA4670 Dominating Patterns AC自动机模板
Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- AC自动机 LA 4670 Dominating Patterns
题目传送门 题意:训练指南P216 分析:求出现最多次数的字串,那么对每个字串映射id,cnt记录次数求最大就可以了. #include <bits/stdc++.h> using nam ...
- UVALive-4670 Dominating Patterns(AC自动机)
题目大意:找出出现次数最多的模式串. 题目分析:AC自动机裸题. 代码如下: # include<iostream> # include<cstdio> # include&l ...
- LA 4670 (AC自动机 模板题) Dominating Patterns
AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...
- uvalive 4670 Dominating Patterns
在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...
- UVa1449 - Dominating Patterns(AC自动机)
题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...
随机推荐
- JS复习:第二十章
一.JSON 1.JSON的语法可以表示以下三种类型的值: (1)简单值:字符串.数值.布尔值和null.如:5,“Hello,World!” (2)对象:javascript中对象字面量: var ...
- HDU 1054 Strategic Game(无向二分图的最大匹配)
( ̄▽ ̄)" //凡无向图,求匹配时都要除以2 #include<iostream> #include<cstdio> #include<algorithm&g ...
- linux之sed命令
原命令行: sudo sed -i 's/${storm.home}\/logs\/var\/log\/storm/g' /usr/share/storm/log4j/storm.log.proper ...
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- 获取table表格的一些不为人知的属性
JS获取表格的简便方法:获取tbody:tBodies 获取thead:tHead 获取tfoot:tFoot 获取行tr:rows 获取列td:cells 使用实例: oTable. ...
- 利用PYTHON设计计算器功能
通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3 ...
- 588. [NOIP1999] 拦截导弹
588. [NOIP1999] 拦截导弹 ★ 输入文件:missile.in 输出文件:missile.out 简单对比 时间限制:1 s 内存限制:128 MB 题目描述 某国为了防御敌国的导 ...
- window.showModalDialog()的简单用法
//创建一个显示html内容的模态对话框: vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) //创建一个 ...
- mysql创建计算列
mysql> create table t(id int auto_increment not null,c1 int,c2 int,c3 int as (c1+c2),primary key( ...
- 在CentOS linux上通过yum安装JDK<转>
卸载centos自带的jdk 1.查看当前的jdk版本,并卸载 [root@localhost opt]# rpm -qa|grep java java-1.6.0-openjdk-1.6.0.3 ...