[UVA1449] Dominating Patterns(AC自动机,STL,计数,神坑)
题目链接:https://vjudge.net/problem/UVA-1449
题意:给一个词典和一个字符串,找出字符串里出现次数最多的单词,如果有出现次数相同的,要按照输入顺序输出。
坑点好多,或许是因为自己实现得比较挫所以坑才多。
用一个map<string,int>计数,map<string,vector<int>>记录同样的字符串出现的位置,map<string,int> p记录id,string数组记录对应id下的字符串。
查询的时候注意假如字符串中并没有出现词典中的单词,输出0,要按顺序把所有字符串打印出来。
太菜了,这个坑WA了十发。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 5 typedef pair<int, int> pii; 6 typedef pair<int, string> pis; 7 const int maxn = 155; 8 const int maxm = 1000100; 9 map<string, int> vis; 10 map<string, vector<int>> id; 11 map<string, int> p; 12 string qq[maxn]; 13 int pcnt; 14 15 typedef class Node { 16 public: 17 Node *next[26]; 18 Node *fail; 19 int flag; 20 int id; 21 Node() { 22 memset(next, 0, sizeof(next)); 23 fail = NULL; 24 flag = 0; 25 id = -1; 26 } 27 }Node; 28 29 class AC_Automation { 30 public: 31 Node *root; 32 queue <Node*> q; 33 AC_Automation() { 34 root = new Node(); 35 while(!q.empty()) q.pop(); 36 } 37 void insert(string s) { 38 Node *cur = root; 39 int len = s.length(); 40 for(int i = 0; i < len; i++) { 41 int index = s[i] - 'a'; 42 if(cur->next[index] == NULL) { 43 cur->next[index] = new Node(); 44 } 45 cur = cur->next[index]; 46 } 47 cur->id = p[s]; cur->flag = 1; 48 } 49 void BuildAC() { 50 Node *cur, *tmp; 51 q.push(root); 52 while(!q.empty()) { 53 cur = q.front(); 54 q.pop(); 55 for(int i = 0; i < 26; i++) { 56 if(!cur->next[i]) continue; 57 if(cur == root) { 58 cur->next[i]->fail = root; 59 } 60 else { 61 tmp = cur->fail; 62 while(tmp != NULL) { 63 if(tmp->next[i]) { 64 cur->next[i]->fail = tmp->next[i]; 65 break; 66 } 67 tmp = tmp->fail; 68 } 69 if(tmp == NULL) { 70 cur->next[i]->fail = root; 71 } 72 } 73 q.push(cur->next[i]); 74 } 75 } 76 } 77 void query(string s) { 78 Node *cur = root, *tmp; 79 int len = s.length(); 80 for(int i = 0; i < len; i++) { 81 int index = s[i] - 'a'; 82 while(cur->next[index] == NULL && cur != root) { 83 cur = cur->fail; 84 } 85 cur = cur->next[index]; 86 if(cur == NULL) { 87 cur = root; 88 continue; 89 } 90 tmp = cur; 91 while(tmp != root) { 92 if(tmp->flag) vis[qq[tmp->id]]++; 93 tmp = tmp->fail; 94 } 95 } 96 int maxx = 0; 97 vector<pis> ret; 98 for(auto it : vis) maxx = max(maxx, it.second); 99 for(auto it : vis) { 100 if(it.second == maxx) { 101 for(auto itt : id[it.first]) { 102 ret.push_back(pis(itt, it.first)); 103 } 104 } 105 } 106 sort(ret.begin(), ret.end()); 107 printf("%d\n", maxx); 108 if(maxx != 0) { 109 for(auto it : ret) { 110 printf("%s\n", it.second.c_str()); 111 } 112 } 113 else { 114 for(int i = 0; i < pcnt; i++) { 115 printf("%s\n", qq[i].c_str()); 116 } 117 } 118 } 119 }; 120 121 char pat[maxn], tar[maxm]; 122 int n; 123 124 int main() { 125 // freopen("in", "r", stdin); 126 while(~scanf("%d", &n) && n) { 127 id.clear(); p.clear(); vis.clear(); 128 pcnt = 0; 129 AC_Automation ac = AC_Automation(); 130 for(int i = 1; i <= n; i++) { 131 scanf("%s", pat); 132 if(p.find(pat) == p.end()) { 133 qq[pcnt] = pat; 134 p[pat] = pcnt++; 135 } 136 id[pat].push_back(i); 137 ac.insert(pat); 138 } 139 ac.BuildAC(); 140 scanf("%s", tar); 141 ac.query(tar); 142 } 143 return 0; 144 }
转载于:https://www.cnblogs.com/kirai/p/6777003.html
[UVA1449] Dominating Patterns(AC自动机,STL,计数,神坑)相关推荐
- LA4670 Dominating Patterns[AC自动机]
The archaeologists are going to decipher a very mysterious "language". Now, they know many ...
- bzoj2754JZOJ2834【SCOI2012】喵星球上的点名 AC自动机+STL
题意比较复杂,就不说了. 感觉这题目拿来恶心人的..连输入都要花费好大功夫.表示这种题目经典套路啊,一般来说直接上AC自动机,暴力把大串往小串上贴,然后直接计算答案就行了,但是这数据大小简直了..算了 ...
- AC自动机加强版 uva 1449 - Dominating Patterns
AC自动机最初作用 一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过. 当然这不是AC自动机的全部作用. 本文就是一例,给出几个单词,查询在text里 ...
- 【BZOJ2754】【codevs2403】喵星球上的点名,AC自动机与STL的狂欢
Time:2016.05.17 Author:xiaoyimi 转载注明出处谢谢 传送门1 传送门2 思路: (本来想在学习AC自动机的时候做这个题的,但发现要用vector,所以就弃了) 由于我们要 ...
- LA_4670_Dominating_Patterns_(AC自动机+map)
描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 字符串-AC自动机(详细图解)
文章目录 AC自动机 原理 模板 例题 HDU-2222Keywords Search HDU-2896病毒侵袭 HDU-3065病毒侵袭持续中 POJ-2778DNA Sequence HDU-22 ...
- AC自动机算法及模板
AC自动机算法及模板 2016-05-08 18:58 226人阅读 评论(0) 收藏 举报 分类: AC自动机(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 关于 ...
- POJ 3691 DNA repair AC自动机 + DP
题意:给你只包含'A','G','T','C'四个字母的n个模板串和1个文本串,问你文本串改变多少个字符就可以使得文本串中没有一个模板串 解题思路: 我们可以知道 dp[i][j] 为文本串到 第i ...
- CDOJ1633 Video Game Combos [AC自动机+dp]
题目地址:http://acm.uestc.edu.cn/problem.php?pid=1633 AC自动机+BFS AC自动机,参见:http://www.cnblogs.com/luna-lov ...
- CodeForces - 1252D Find String in a Grid(AC自动机)
题目链接:点击查看 题目大意:给出一个 n * m 的字符矩阵,再给出 q 次询问,每次询问需要回答给出字符串在字符矩阵中出现了多少次,规定在字符矩阵中查找某个字符串,只能先向右 a 个单位,再向下 ...
最新文章
- SOC,System on-a-Chip技术初步
- Ubuntu 10.10系统安装手记
- 幕后我最“牛” | 数据派优秀志愿者风采展
- WinDBg定位asp.net mvc项目异常崩溃源码位置
- 【水】uva10037 过桥
- Android学习之网上商城(下)
- Leetcode每日一题:328.odd-even-linked-list(奇偶链表)
- 1.深入分布式缓存:从原理到实践 --- 缓存为王
- 解决 大漠测试工具 “你的系统没有发现大漠插件”问题
- 【51单片机快速入门指南】6.2:SPI 、八线、四线控制 LCD12864 屏幕及Proteus的仿真(支持中文汉字)
- oracle卸载步骤图解,oracle卸载步骤
- 蔡司数码视疲劳测试软件_数码视觉疲劳,你不知道的蓝光
- AndroidX全解析
- 一个 SAP 开发工程师的 2022 年终总结:四十不惑
- 中国石油大学(北京)-《 油层物理》第二阶段在线作业
- ubuntu卷组安装
- lasso最小角_Python中的Lasso回归之最小角算法LARS
- 微机原理与接口技术重要的知识点
- Confluence 6 管理协同编辑 - 修改你的 Synchrony 配置
- PF_PACKET环形接收缓存
热门文章
- ppd文件下载 linux,Linux系统R230,R270,R330,L380打印机驱动下载爱普生喷墨打印机PPD文件...
- 题解 【NOIP2016】魔法阵
- 回顾12306 成长的烦恼
- Python烤地瓜案例
- Vue基础渲染及修饰符
- 北卡州立大学的数据分析学硕士项目排名大数据专业第一名
- 习题5.4 找出4*5矩阵中值最小和最大元素,并分别输出其值及所在的行号和列号。
- arm mali 天梯图_11月最新版联发科CPU天梯图 直观看天梯图秒懂联发科处理器排行...
- 清明祭曾祖@20130402
- 3.3.3 Camelot亚瑟王的宫殿 USACO