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, 1 ≤ N ≤ 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 to 1E6
.
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加上离散一下(是离散?不清楚,汝佳用的map,某大神用的hash,我就直接存下来了每次输入的所有单词并且存下来了这个单词对应的叶子节点,到时候直接访问对比tot,两次分开的n的循环就可以了。这里的重复出现的单词我个人认为就用出现的顺序来区分很方便,因为在tot上覆盖就覆盖啊,反正最后得到的tot两个相同的单词都一样的……
第一次完全脱离模板写AC,还有一点不熟练,把有的细节忘了写了,要多熟练一下。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn=150*70+5;
char s[155][75],ov[1000005];
int n,ch[maxn][30],len,inde,sig[maxn],last[maxn],mapy[155],ans,tot[maxn],nxt[maxn];
int calc(char c)
{return c-'a';
}
void insert(int cur)
{len=strlen(s[cur]);int u=0;for(int i=0;i<len;i++){if(!ch[u][calc(s[cur][i])])ch[u][calc(s[cur][i])]=++inde;u=ch[u][calc(s[cur][i])];}sig[u]++;mapy[cur]=u;
}
void getfail()
{queue<int>q;nxt[0]=0;for(int i=0;i<26;i++)if(ch[0][i])//0不处理...因为好像本来跑回0也没有问题?q.push(ch[0][i]);while(!q.empty()){int cur=q.front();q.pop();for(int i=0;i<26;i++)if(ch[cur][i]){int u=ch[cur][i];q.push(u);int j=nxt[cur];while(j&&!ch[j][i])j=nxt[j];nxt[u]=ch[j][i];last[u]=sig[nxt[u]]?nxt[u]:last[nxt[u]];}else ch[cur][i]=ch[nxt[cur]][i];}
}
void init()
{clr(ch);clr(nxt);clr(last);clr(tot);inde=0;ans=0;for(int i=1;i<=n;i++){scanf("%s",s[i]);insert(i);}scanf("%s",ov);getfail();
}
void print(int j)
{if(j){tot[j]++;print(last[j]);}
}
void mat()
{int j=0;len=strlen(ov);for(int i=0;i<len;i++){j=ch[j][calc(ov[i])];if(sig[j])print(j);else print(last[j]);}for(int i=1;i<=n;i++)ans=max(ans,tot[mapy[i]]);printf("%d\n",ans);for(int i=1;i<=n;i++)if(tot[mapy[i]]==ans)printf("%s\n",s[i]);
}
int main()
{freopen("LA4670.in","r",stdin);freopen("LA4670.out","w",stdout);scanf("%d",&n);//for(scanf("%d",&n);n;scanf("%d",&n))while(n){init();mat();scanf("%d",&n);}return 0;
}

LA4670 Dominating Patterns[AC自动机]相关推荐

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

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

  2. LA_4670_Dominating_Patterns_(AC自动机+map)

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

  3. CDOJ1633 Video Game Combos [AC自动机+dp]

    题目地址:http://acm.uestc.edu.cn/problem.php?pid=1633 AC自动机+BFS AC自动机,参见:http://www.cnblogs.com/luna-lov ...

  4. KMP算法、AC自动机算法的原理介绍以及Python实现

    KMP算法 要弄懂AC自动机算法,首先弄清楚KMP算法. 这篇文章讲的很好: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E ...

  5. hihocoder第218周:AC自动机

    题目链接 问题描述 给定n个单词,给定一个长字符串s,单词总长度和字符串s的长度都不超过1e5.要求把s中所有的出现单词的位置用*替代. 例如: 样例输入 2 abc cd abcxyzabcd 样例 ...

  6. 2018北京ICPC H. Approximate Matching(AC自动机+DP)

    H : Approximate Matching 时间限制:1000ms,单点时限:1000ms,内存限制:512MB 描述 String matching, a common problem in ...

  7. HDU 6208 The Dominator of Strings AC自动机

    题目链接:HDU 6208 The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535 ...

  8. HDU 6208【假AC自动机+string方法】

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=1 The Dominator of Strings Time Limit: 3000/3000 MS ( ...

  9. Aho-Corasick 多模式匹配算法(AC自动机) 的算法详解及具体实现

    多模式匹配 多模式匹配就是有多个模式串P1,P2,P3-,Pm,求出所有这些模式串在连续文本T1-.n中的所有可能出现的位置. 例如:求出模式集合{"nihao","ha ...

最新文章

  1. mysql.func_Mysqlfunc.c
  2. java中调用kettle作业以及生成web service 接口
  3. 技术有温,代码有爱——1024技术公益信息无障碍
  4. 【转】spin_lock、spin_lock_irq、spin_lock_irqsave区别
  5. php 依赖注入框架,依赖注入模式(Dependency Injection)
  6. ffmpeg.exe dos下怎么用 放在哪里
  7. redis 哨兵_Redis的哨兵模式
  8. 使用SQL Server事务复制将SQL Server数据库迁移到Azure SQL数据库
  9. Linux MySQL主主复制(Replication)(MySQL数据双向同步)配置
  10. Android 和 PHP 之间进行数据加密传输
  11. sklearn中的naive bayes算法
  12. 【数学基础】一份非常适合人工智能学习的线性代数基础材料中文版 (国内教材精华)...
  13. 关于培训机构~程序员培训
  14. 荣耀magicbook笔记本BIOS设置
  15. yum install docker 安装的是 podman-docker而不是 docker
  16. 金山卫士界面源码解读及界面库分离(4)
  17. 【Python黑科技】图片太大不能上传?三种压缩图片大小的方法(代码注释详细)
  18. 尚融宝28-投资列表展示
  19. 剪贴板查看器:CopyClip 2 for Mac
  20. ​谁是信创担当——《2021中国信创生态市场研究报告》正式发布

热门文章

  1. Select2数据回显
  2. jq导航栏点击滚动到对应位置 导航栏随页面滑动变化对应导航颜色
  3. java毕业生设计采购物料质量检验系统计算机源码+系统+mysql+调试部署+lw
  4. 新手学画画应该如何上色?上色有什么技巧!
  5. egg 如何配置 cors(全网少数配完可行的文档)
  6. 临床研究有哪些常见类型?
  7. 云朵子 -- 云朵君微信表情包第一弹新鲜出炉
  8. 认识EMC Unity 存储设备的控制器SP
  9. Java实时监控文件夹变化
  10. webpack的resolve.modules和 resolve.alias有什么区别