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自动机入门题的更多相关文章

  1. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  2. HDU2222(AC自动机入门题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. hdu2222之AC自动机入门

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

    /** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...

  5. hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,

    /** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...

  6. UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机

    /** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...

  7. UVALive-4670 AC自动机入门题 求出现次数最多的子串

    /** 链接:http://vjudge.net/problem/UVALive-4670 详见lrj训练指南P216 */ #include<bits/stdc++.h> using n ...

  8. HDU3065(AC自动机入门题)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  9. HDU2896(AC自动机入门题)

    病毒侵袭 Time Limit:1000MS     Memory Limit:32768KB   Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...

随机推荐

  1. 添加asp.net mvc到现有的asp.net web form 应用程序

    前言 asp.net mvc的前一版本为asp.net web Form(Asp.net mvc之前称为asp.net),其第一个版本与2002年年初发布.asp.net web form 属于.ne ...

  2. 2013腾讯编程马拉松||HDU 4505 小Q系列故事——电梯里的爱情 水水水

    http://acm.hdu.edu.cn/showproblem.php?pid=4505 题目大意: 电梯最开始在0层,并且最后必须再回到0层才算一趟任务结束.假设在开始的时候已知电梯内的每个人要 ...

  3. Testfan软件测试社区

    1.  http://ask.testfan.cn/article/902  Appium 服务端安装-windows2.  http://ask.testfan.cn/article/1078 最新 ...

  4. DC中为什么要用Uniquify?

    转自:http://blog.sina.com.cn/s/blog_68c493870101exl7.html 为了在layout中进行时钟树的综合,网表在DC中必须被uniquified.所谓uni ...

  5. 第一个hello word 驱动载入失败--------

    今天尝试自己载入第一个驱动模块,依据惯例hello word 然后失败了,如今说明我的操作过程.请个位看看. 首先我的内核版本号: 模块代码与MAKEFILE #include<linux/in ...

  6. Spring5源码深度解析(一)之理解Configuration注解

    代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian 1.Spring体系结构 ...

  7. POJ 1595 Prime Cuts (ZOJ 1312) 素数打表

    ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...

  8. 10.11 android输入系统_补充知识_activity_window_decor_view关系

    android里:1个application, 有1个或多个activity(比如支付宝有:首页.财富.口碑.朋友.我的,这些就是activity)1个activity, 有1个window(每个ac ...

  9. iOS调试 - 基本技巧

    在程序中,无论是你想弄清楚为什么数组中有3个对象而不是5个,或者为什么一个新的玩家开始之后,游戏在倒退——调试在这些处理过程中是比较重要的一部 分.通过本文的学习,我们将知道在程序中,可以使用的大多数 ...

  10. 使用URLConnection获取网页信息的基本流程 分类: H1_ANDROID 2013-10-12 23:51 3646人阅读 评论(0) 收藏

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...