UVALive 4670 Dominating Patterns(AC自动机)
题意:给出n个串,再给出一个文本串T,问哪些串在文本串中出现的次数最多。
思路:直接AC自动机搞,感觉还是比较裸的,每次遇到结尾的单词就给这个串出现次数+1。看完Trie和KMP感觉AC自动机还是蛮好理解的,之前看了看算导,感觉KMP和AC自动机都是一种有限自动机,把状态转移图弄好了就ok~
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=200;
const int maxm=100+10;
const int maxl=1000000+10;
char pat[maxn][maxm],str[maxl];
int ch[20010][26],val[20010],size,lastv[20010],next[20010];
int cnt[maxn],maxv;
void Init()
{memset(cnt,0,sizeof(cnt));memset(ch[0],0,sizeof(ch[0]));val[0]=0;size=0;maxv=0;
}
void insert(const char * s,int v)
{int u=0,n=strlen(s);for(int i=0;i<n;++i){int c=s[i]-'a';if(!ch[u][c]){ch[u][c]=++size;memset(ch[size],0,sizeof(ch[size]));val[size]=0;}u=ch[u][c];}val[u]=v;
}
void build()
{queue<int>q;memset(lastv,0,sizeof(lastv));memset(next,0,sizeof(next));for(int i=0;i<26;++i)if(ch[0][i]) {q.push(ch[0][i]);}while(!q.empty()){int u=q.front();q.pop();for(int c=0;c<26;++c){int v=ch[u][c];if(!v) {ch[u][c]=ch[next[u]][c];continue;}q.push(v);int j=next[u];while(j&&!ch[j][c]) j=next[j];next[v]=ch[j][c];lastv[v]=val[next[v]]?next[v]:lastv[next[v]];}}
}
void find(const char * s)
{int n=strlen(s);int j=0;for(int i=0;i<n;++i){int c=s[i]-'a';j=ch[j][c];if(val[j]||lastv[j]){int u=val[j]?j:lastv[j];while(u){cnt[val[u]]++;maxv=max(maxv,cnt[val[u]]);u=lastv[u];}}}
}
int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int n;map<string,int>mp;while(~scanf("%d",&n)){if(n==0) break;mp.clear();Init();int tmp;for(int i=1;i<=n;++i){scanf("%s",pat[i]);tmp=mp[string(pat[i])];if(tmp) mp[string(pat[i])]=tmp;else{mp[string(pat[i])]=i;insert(pat[i],i);}}build();scanf("%s",str);find(str);printf("%d\n",maxv);for(int i=1;i<=n;++i){tmp=mp[string(pat[i])];if(cnt[tmp]==maxv)printf("%s\n",pat[i]);}}return 0;
}
UVALive 4670 Dominating Patterns(AC自动机)相关推荐
- LA4670 Dominating Patterns[AC自动机]
The archaeologists are going to decipher a very mysterious "language". Now, they know many ...
- LA_4670_Dominating_Patterns_(AC自动机+map)
描述 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- AC自动机加强版 uva 1449 - Dominating Patterns
AC自动机最初作用 一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过. 当然这不是AC自动机的全部作用. 本文就是一例,给出几个单词,查询在text里 ...
- UVALive 4126 Password Suspects(AC自动机+dp)
题意就是长度为n的串,满足下列m个串是它的子串,问这个串有多少种,如果少于42种则字典序输出. dp[i][j][k]为,填充了i个字符,在ac自动机的j节点上,字串集合为k的方案数,填充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 ...
- KMP算法、AC自动机算法的原理介绍以及Python实现
KMP算法 要弄懂AC自动机算法,首先弄清楚KMP算法. 这篇文章讲的很好: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E ...
- hihocoder第218周:AC自动机
题目链接 问题描述 给定n个单词,给定一个长字符串s,单词总长度和字符串s的长度都不超过1e5.要求把s中所有的出现单词的位置用*替代. 例如: 样例输入 2 abc cd abcxyzabcd 样例 ...
- 2018北京ICPC H. Approximate Matching(AC自动机+DP)
H : Approximate Matching 时间限制:1000ms,单点时限:1000ms,内存限制:512MB 描述 String matching, a common problem in ...
- HDU 6208 The Dominator of Strings AC自动机
题目链接:HDU 6208 The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535 ...
- HDU 6208【假AC自动机+string方法】
题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=1 The Dominator of Strings Time Limit: 3000/3000 MS ( ...
最新文章
- 【v2.x OGE-example 第一节】 绘制实体
- halcon知识:共生矩阵
- JS 异常: Uncaught RangeError: Maximum call stack size exceeded
- PlateSpin 完全复制由于LVM没有可用空闲空间导致失败
- Taro+react开发(24)--this.state和this.props
- (转)使用异步Python 3.6和Redis编写快速应用程序
- 2025年的呼叫中心是什么样的?
- 如何开启jvm日志_Java 从小白到大牛,JVM 不得不知的一些参数和配置
- unity3D-Gear VR字体由小变大效果
- 《学习之道》第十二章练习困难的部分
- MySQL不能使用/tmp
- 关于来料检验中多退货的问题
- 大数据可视化技术应用学习目标与复习小结
- Pointer Networks简介及其应用
- TJA1044---具有待机模式的高速CAN收发器
- PHP 调用浏览器下载文件
- 113.输入10个国家的名字,按由小到大排序
- DIV CSS 鼠标事件
- check the manual that corresponds to your MySQL server version for the right syntax to use near (0)
- HuggingFace简明教程
热门文章
- GoLand Jetbrain工具使用
- 一个3D城市地图应用开发工具,等你获取 ThingJS 3D 全景 可视化
- java批处理查询_java 实现批量查询
- 我的markdown
- 学霸的迷宫 - 广搜例题
- 清华学霸尹成Python教程
- excel切片器_Excel有一对极品组合工具,能快速实现数据多维动态透视
- const T 与T const(const T vs.T const的翻译 Dan Saks)
- 针对VMware安装Win10需要安装.NET Framework 3.5常见的0x800F0906、0x800F081F错误解决办法
- Atom-beautify插件的安装,使用过程