题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次。

析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define debug puts("+++++")
  17. //#include <tr1/unordered_map>
  18. #define freopenr freopen("in.txt", "r", stdin)
  19. #define freopenw freopen("out.txt", "w", stdout)
  20. using namespace std;
  21. //using namespace std :: tr1;
  22.  
  23. typedef long long LL;
  24. typedef pair<int, int> P;
  25. const int INF = 0x3f3f3f3f;
  26. const double inf = 0x3f3f3f3f3f3f;
  27. const LL LNF = 0x3f3f3f3f3f3f;
  28. const double PI = acos(-1.0);
  29. const double eps = 1e-8;
  30. const int maxn = 1e6 + 5;
  31. const LL mod = 2147493647;
  32. const int N = 1e6 + 5;
  33. const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
  34. const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
  35. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  36. inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
  37. inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
  38. inline int lcm(int a, int b){ return a * b / gcd(a, b); }
  39. int n, m;
  40. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  42. inline int Min(int a, int b){ return a < b ? a : b; }
  43. inline int Max(int a, int b){ return a > b ? a : b; }
  44. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  45. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  46. inline bool is_in(int r, int c){
  47. return r >= 0 && r < n && c >= 0 && c < m;
  48. }
  49. const int sigma = 26;
  50. const int maxnode = 70 * 150 + 5;
  51.  
  52. struct Aho{
  53. int cnt[155];
  54. int ch[maxnode][sigma];
  55. int f[maxnode];
  56. int val[maxnode];
  57. int last[maxnode];
  58. int sz;
  59. void init(){
  60. sz = 1;
  61. memset(ch[0], 0, sizeof ch[0]);
  62. memset(cnt, 0, sizeof cnt);
  63. }
  64.  
  65. int idx(char ch){ return ch - 'a'; }
  66.  
  67. void print(int j){
  68. if(j){
  69. ++cnt[val[j]];
  70. print(last[j]);
  71. }
  72. }
  73. void insert(char *s, int v){
  74. int u = 0;
  75. while(*s){
  76. int c = idx(*s);
  77. if(!ch[u][c]){
  78. memset(ch[sz], 0, sizeof ch[sz]);
  79. val[sz] = 0;
  80. ch[u][c] = sz++;
  81. }
  82. u = ch[u][c]; ++s;
  83. }
  84. val[u] = v;
  85.  
  86. }
  87.  
  88. void find(char *s){
  89. int j = 0;
  90. while(*s){
  91. int c = idx(*s);
  92. while(j && !ch[j][c]) j = f[j];
  93. j = ch[j][c];
  94. if(val[j]) print(j);
  95. else if(last[j]) print(last[j]);
  96. ++s;
  97. }
  98. }
  99.  
  100. void getFail(){
  101. queue<int> q;
  102. f[0] = 0;
  103. for(int c = 0; c < sigma; ++c){
  104. int u = ch[0][c];
  105. if(u){ f[u] = 0; q.push(u); last[u] = 0; }
  106. }
  107.  
  108. while(!q.empty()){
  109. int r = q.front(); q.pop();
  110. for(int c = 0;c < sigma; ++c){
  111. int u = ch[r][c];
  112. if(!u) continue;
  113. q.push(u);
  114. int v = f[r];
  115. while(v && !ch[v][c]) v = f[v];
  116. f[u] = ch[v][c];
  117. last[u] = val[f[u]] ? f[u] : last[f[u]];
  118. }
  119. }
  120. }
  121. };
  122.  
  123. Aho aho;
  124. char s[155][75];
  125. char t[1000005];
  126. map<string, int> mp;
  127.  
  128. int main(){
  129. while(scanf("%d", &n) == 1 && n){
  130. aho.init();
  131. mp.clear();
  132. for(int i = 1; i <= n; ++i){
  133. scanf("%s", s[i]);
  134. aho.insert(s[i], i);
  135. mp[s[i]] = i;
  136. }
  137. scanf("%s", t);
  138. aho.getFail();
  139. aho.find(t);
  140. int mmax = -1;
  141. for(int i = 1; i <= n; ++i) mmax = Max(mmax, aho.cnt[i]);
  142. printf("%d\n", mmax);
  143. for(int i = 1; i <= n; ++i)
  144. if(aho.cnt[mp[s[i]]] == mmax) printf("%s\n", s[i]);
  145. }
  146. return 0;
  147. }

LA 4670 Dominating Patterns (AC自动机)的更多相关文章

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

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

  2. UVALive - 4670 Dominating Patterns AC 自动机

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

  3. LA 4670 Dominating Patterns (AC自动机)

    题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多. 析:一匹配多,很明显是AC自动机.只需要对原来的进行修改一下,就可以得到这个题的答案, 计算过程中,要更新次数,并且要映射字符串.如 ...

  4. UVa1449 - Dominating Patterns(AC自动机)

    题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...

  5. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  6. LA4670 Dominating Patterns AC自动机模板

    Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...

  7. AC自动机 LA 4670 Dominating Patterns

    题目传送门 题意:训练指南P216 分析:求出现最多次数的字串,那么对每个字串映射id,cnt记录次数求最大就可以了. #include <bits/stdc++.h> using nam ...

  8. UVa Live 4670 Dominating Patterns - Aho-Corasick自动机

    题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...

  9. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

随机推荐

  1. LCA 在线倍增法 求最近公共祖先

    第一步:建树  这个就不说了 第二部:分为两步  分别是深度预处理和祖先DP预处理 DP预处理: int i,j; ;(<<j)<n;j++) ;i<n;++i) ) fa[i ...

  2. 离线配置Anaconda3+tensorflow-gpu1.4.0+cuda8.0+cudnn6.0

    1.首先下载anaconda3 ----从官网上下载Anaconda3-5.1.0-Linux-x86_64.sh 直接通过命令 bash Anaconda3-5.1.0-Linux-x86_64.s ...

  3. 某考试 T1 arg

    题目描述 给出一个长度为 m 的序列 A, 请你求出有多少种 1...n 的排列, 满足 A 是它的一个 LIS. 输入格式 第一行两个整数 n, m. 接下来一行 m 个整数, 表示 A. 输出格式 ...

  4. asterisk 通道变量

    ${ACCOUNTCODE}: 用户计费帐号 sip.conf 里的 account=XXXX ${ANSWEREDTIME}: 通话时长(秒) ${BLINDTRANSFER}: 通道是否为转接类型 ...

  5. 下载数据到Excel,工具类

    使用反射将model数据下载到Excel中 package test.upload.utils; import java.lang.reflect.Method; import java.math.B ...

  6. 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...

  7. iOS开发-UITableView单选多选/复选实现1

    TableView怎样实现单选或者多选呢? 我们的直接思路是改动某一个Cell的样式就可以, 那么改动样式须要通过改动相应的数据, 从这里能够判断我们须要给Cell相应的数据设置一个标志位, 当选中的 ...

  8. Python的字符串和列表和字典的方法/函数

    字符串 S.find()#可指定范围查找字串,返回索引值,否则返回-1 S.index()#同find,只是找不到的之后返回异常 S.count()#返回找到字串的个数 S.lower()#转小写 S ...

  9. ascii与unicode,utf-8小结

    ascii是以一个字节存储英文和特殊字符,不支持中文的处理.unicode占用的是两个字节,可以存储中文.utf-8占用三个字节,可以根据存储的内容进行中英文的转换. Python的解释器是不支持中文 ...

  10. 搜索学术论文訪问google的能用的几个IP地址

    google搜索引擎打不开时的解决的方法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过測试可用的IP,用来在不能域名訪问google的时候进行訪问 更新一个最新的. ...