【hdu2222】【poj2945】AC自动机入门题
HDU2222 传送门
题目分析
裸题:注意构建自动机用的是模式串,思想和kmp很类似。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e4 + 5, M = 55, L = 1e6 + 5;
int T, n, tot, vst[N * M], vt;
char t[M], s[L];
struct node{
int trans[30], fail;
bool end;
inline void clear(){
fail = 0;
memset(trans, 0, sizeof trans);
end = false;
}
}trie[N * M];
inline void insert(){
int pos = 1, len = strlen(t + 1);
for(int i = 1; i <= len; i++){
if(!trie[pos].trans[t[i] - 'a' + 1])
trie[trie[pos].trans[t[i] - 'a' + 1] = ++tot].clear();
pos = trie[pos].trans[t[i] - 'a' + 1];
}
trie[pos].cnt++;
}
inline void buildFail(){
static int qn, que[N * M];
que[qn = 1] = 1;
for(int ql = 1; ql <= qn; ql++){
int u = que[ql];
for(int i = 1; i <= 26; i++){
int v = trie[u].fail;
while(!trie[v].trans[i]) v = trie[v].fail;
v = trie[v].trans[i];
int w = trie[u].trans[i];
if(w) trie[w].fail = v, que[++qn] = w;
else trie[u].trans[i] = v;
}
}
}
int main(){
scanf("%d", &T);
for(int i = 1; i <= 26; i++) trie[0].trans[i] = 1;
while(T--){
++vt;
trie[tot = 1].clear();
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%s", t + 1);
insert();
}
buildFail();
scanf("%s", s + 1);
int len = strlen(s + 1), tmp, now = 1, ans = 0;
for(int i = 1; i <= len; i++){
now = trie[now].trans[s[i] - 'a' + 1];
tmp = now;
while(tmp && vst[tmp] != vt){
vst[tmp] = vt;
ans += trie[tmp].cnt;
tmp = trie[tmp].fail;
}
}
printf("%d\n", ans);
}
}
POJ2945
题目分析
还是裸题,在字符串结束的地方打上标记
code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2e4 + 5, M = 25;
int n, m, ans[N];
struct node{
node* trans[5];
int cnt;
}trie[N * M], *tail = trie, *root;
char s[M];
inline node* newNode(){
node *x = tail++;
memset(x->trans, 0, sizeof x->trans);
x->cnt = 0;
return x;
}
inline int getVal(char t){
switch(t){
case 'A': return 1;
case 'C': return 2;
case 'G': return 3;
case 'T': return 4;
}
}
inline void insert(){
node *pos = root;
for(int i = 1; i <= m; i++){
int v = getVal(s[i]);
if(pos->trans[v] == NULL)
pos->trans[v] = newNode();
pos = pos->trans[v];
}
ans[pos->cnt]--;
ans[++pos->cnt]++;
}
int main(){
while(scanf("%d%d", &n, &m), n + m){
memset(ans, 0, sizeof ans);
tail = trie;
root = newNode();
for(int i = 1; i <= n; i++){
scanf("%s", s + 1);
insert();
}
for(int i = 1; i <= n; i++) printf("%d\n", ans[i]);
}
}
【hdu2222】【poj2945】AC自动机入门题的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222之AC自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...
- UVALive-4670 AC自动机入门题 求出现次数最多的子串
/** 链接:http://vjudge.net/problem/UVALive-4670 详见lrj训练指南P216 */ #include<bits/stdc++.h> using n ...
- HDU3065(AC自动机入门题)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU2896(AC自动机入门题)
病毒侵袭 Time Limit:1000MS Memory Limit:32768KB Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...
随机推荐
- Mongodb总结3-稍微封装一下
上次发表的2,纯粹是Demo,演示API的用法. 今天,稍微封装了下,看得更清楚. 考虑到不容易做得很有通用性,所以封装的一般,换种场景需要直接修改代码,但是有一部分是可以复用的. 最近项目,很可能只 ...
- Cocos2d-x学习笔记(一)HelloWorld
原创文章,转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38656755 前言 正式来公司实习已有一月,前一月主要是看了<C ...
- 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例
(本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...
- Altium Designer如何改两个原件之间的安全距离
在pcb中按 D R 一个事垂直距离, 另一个是水平距离.
- Altium Designer如何调整鼠标形状
在 里面有一个
- Scala在挖财的应用实践--转载
原文地址:http://www.infoq.com/cn/articles/scala-architecture-wacai 编者按:本文是根据ArchSummit大会上挖财资深架构师王宏江的演讲&l ...
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect(转)
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.通过配置,我们配置某个 ViewResolver 如下: <b ...
- js中退出语句break,continue和return 比较 (转)
在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止 首先:break和continue两个一 ...
- CSS vertical-align属性的使用方法
这两天写个页面css的时候用到了vertical-align属性.使用过程中踩到了坑,所以总结例如以下: vertical-align的定义 W3C上对vertical-align的定义:vertic ...
- flash stm32的flash编写
定义一个全局变量数组:const u8 TEXT_Buffer[]={"STM32F103 FLASH TEST"}; //u8和char* 写入到内存里会有什么区别???? ...