LA 4670 Dominating Patterns (AC自动机)
题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次。
析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好。
代码如下:
- #pragma comment(linker, "/STACK:1024000000,1024000000")
- #include <cstdio>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <cstring>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <cctype>
- #include <cmath>
- #include <stack>
- #define debug puts("+++++")
- //#include <tr1/unordered_map>
- #define freopenr freopen("in.txt", "r", stdin)
- #define freopenw freopen("out.txt", "w", stdout)
- using namespace std;
- //using namespace std :: tr1;
- typedef long long LL;
- typedef pair<int, int> P;
- const int INF = 0x3f3f3f3f;
- const double inf = 0x3f3f3f3f3f3f;
- const LL LNF = 0x3f3f3f3f3f3f;
- const double PI = acos(-1.0);
- const double eps = 1e-8;
- const int maxn = 1e6 + 5;
- const LL mod = 2147493647;
- const int N = 1e6 + 5;
- const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
- const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
- const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
- inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
- inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
- inline int lcm(int a, int b){ return a * b / gcd(a, b); }
- int n, m;
- const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- inline int Min(int a, int b){ return a < b ? a : b; }
- inline int Max(int a, int b){ return a > b ? a : b; }
- inline LL Min(LL a, LL b){ return a < b ? a : b; }
- inline LL Max(LL a, LL b){ return a > b ? a : b; }
- inline bool is_in(int r, int c){
- return r >= 0 && r < n && c >= 0 && c < m;
- }
- const int sigma = 26;
- const int maxnode = 70 * 150 + 5;
- struct Aho{
- int cnt[155];
- int ch[maxnode][sigma];
- int f[maxnode];
- int val[maxnode];
- int last[maxnode];
- int sz;
- void init(){
- sz = 1;
- memset(ch[0], 0, sizeof ch[0]);
- memset(cnt, 0, sizeof cnt);
- }
- int idx(char ch){ return ch - 'a'; }
- void print(int j){
- if(j){
- ++cnt[val[j]];
- print(last[j]);
- }
- }
- void insert(char *s, int v){
- int u = 0;
- while(*s){
- int c = idx(*s);
- if(!ch[u][c]){
- memset(ch[sz], 0, sizeof ch[sz]);
- val[sz] = 0;
- ch[u][c] = sz++;
- }
- u = ch[u][c]; ++s;
- }
- val[u] = v;
- }
- void find(char *s){
- int j = 0;
- while(*s){
- int c = idx(*s);
- while(j && !ch[j][c]) j = f[j];
- j = ch[j][c];
- if(val[j]) print(j);
- else if(last[j]) print(last[j]);
- ++s;
- }
- }
- void getFail(){
- queue<int> q;
- f[0] = 0;
- for(int c = 0; c < sigma; ++c){
- int u = ch[0][c];
- if(u){ f[u] = 0; q.push(u); last[u] = 0; }
- }
- while(!q.empty()){
- int r = q.front(); q.pop();
- for(int c = 0;c < sigma; ++c){
- int u = ch[r][c];
- if(!u) continue;
- q.push(u);
- int v = f[r];
- while(v && !ch[v][c]) v = f[v];
- f[u] = ch[v][c];
- last[u] = val[f[u]] ? f[u] : last[f[u]];
- }
- }
- }
- };
- Aho aho;
- char s[155][75];
- char t[1000005];
- map<string, int> mp;
- int main(){
- while(scanf("%d", &n) == 1 && n){
- aho.init();
- mp.clear();
- for(int i = 1; i <= n; ++i){
- scanf("%s", s[i]);
- aho.insert(s[i], i);
- mp[s[i]] = i;
- }
- scanf("%s", t);
- aho.getFail();
- aho.find(t);
- int mmax = -1;
- for(int i = 1; i <= n; ++i) mmax = Max(mmax, aho.cnt[i]);
- printf("%d\n", mmax);
- for(int i = 1; i <= n; ++i)
- if(aho.cnt[mp[s[i]]] == mmax) printf("%s\n", s[i]);
- }
- return 0;
- }
LA 4670 Dominating Patterns (AC自动机)的更多相关文章
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- UVALive - 4670 Dominating Patterns AC 自动机
input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...
- LA 4670 Dominating Patterns (AC自动机)
题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多. 析:一匹配多,很明显是AC自动机.只需要对原来的进行修改一下,就可以得到这个题的答案, 计算过程中,要更新次数,并且要映射字符串.如 ...
- UVa1449 - Dominating Patterns(AC自动机)
题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...
- UVa 1449 - Dominating Patterns (AC自动机)
题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...
- LA4670 Dominating Patterns AC自动机模板
Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...
- AC自动机 LA 4670 Dominating Patterns
题目传送门 题意:训练指南P216 分析:求出现最多次数的字串,那么对每个字串映射id,cnt记录次数求最大就可以了. #include <bits/stdc++.h> using nam ...
- UVa Live 4670 Dominating Patterns - Aho-Corasick自动机
题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns 题目: Dominating Patterns Time Limit: 3000MS Memory Limit: Unkn ...
随机推荐
- LCA 在线倍增法 求最近公共祖先
第一步:建树 这个就不说了 第二部:分为两步 分别是深度预处理和祖先DP预处理 DP预处理: int i,j; ;(<<j)<n;j++) ;i<n;++i) ) fa[i ...
- 离线配置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 ...
- 某考试 T1 arg
题目描述 给出一个长度为 m 的序列 A, 请你求出有多少种 1...n 的排列, 满足 A 是它的一个 LIS. 输入格式 第一行两个整数 n, m. 接下来一行 m 个整数, 表示 A. 输出格式 ...
- asterisk 通道变量
${ACCOUNTCODE}: 用户计费帐号 sip.conf 里的 account=XXXX ${ANSWEREDTIME}: 通话时长(秒) ${BLINDTRANSFER}: 通道是否为转接类型 ...
- 下载数据到Excel,工具类
使用反射将model数据下载到Excel中 package test.upload.utils; import java.lang.reflect.Method; import java.math.B ...
- 报错: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 ...
- iOS开发-UITableView单选多选/复选实现1
TableView怎样实现单选或者多选呢? 我们的直接思路是改动某一个Cell的样式就可以, 那么改动样式须要通过改动相应的数据, 从这里能够判断我们须要给Cell相应的数据设置一个标志位, 当选中的 ...
- Python的字符串和列表和字典的方法/函数
字符串 S.find()#可指定范围查找字串,返回索引值,否则返回-1 S.index()#同find,只是找不到的之后返回异常 S.count()#返回找到字串的个数 S.lower()#转小写 S ...
- ascii与unicode,utf-8小结
ascii是以一个字节存储英文和特殊字符,不支持中文的处理.unicode占用的是两个字节,可以存储中文.utf-8占用三个字节,可以根据存储的内容进行中英文的转换. Python的解释器是不支持中文 ...
- 搜索学术论文訪问google的能用的几个IP地址
google搜索引擎打不开时的解决的方法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过測试可用的IP,用来在不能域名訪问google的时候进行訪问 更新一个最新的. ...