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. python之经典猜数字

    题目:猜数字1.让用户输入1-20,猜数字,可以猜5次.2.每次有提示,大了,或者小了!3.如果超过5次,提示game over. # !/usr/bin/env python # -*- codin ...

  2. !!在JS中代表什么

    !!一般用来将后面的表达式转换为布尔型的数据(boolean), javascript约定和c类似,规则为 ·false.undefinded.null.0."" 为 false, ...

  3. Notepad++和MinGW的安装和配置

    http://blog.csdn.net/cclovepl/article/details/70568313 http://blog.csdn.net/cclovepl/article/details ...

  4. CSS笔记 - fgm练习 - 三个div变色 - CSS div等分布局

    <title>三个div变红</title> <style> *{margin: 0; padding: 0} body{ text-align: center; ...

  5. 51nod Bash游戏(V1,V2,V3,V4(斐波那契博弈))

    Bash游戏V1 有一堆石子共同拥有N个. A B两个人轮流拿.A先拿.每次最少拿1颗.最多拿K颗.拿到最后1颗石子的人获胜.如果A B都很聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得 ...

  6. 通过量产解决U盘写保护,无法格式化问题

    1.首先下载ChipGenius.地址:http://pan.baidu.com/s/1eQvf1zc 2.解压,双击ChipGenius_v4_00_0027_pre2. 3.能够检測到U盘的主控厂 ...

  7. SpringBoot学习:获取yml和properties配置文件的内容(转)

    项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...

  8. sql海量数据优化

    1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设 ...

  9. (十)RabbitMQ消息队列-高可用集群部署实战

    原文:(十)RabbitMQ消息队列-高可用集群部署实战 前几章讲到RabbitMQ单主机模式的搭建和使用,我们在实际生产环境中出于对性能还有可用性的考虑会采用集群的模式来部署RabbitMQ. Ra ...

  10. Kinect 骨骼映射---Let me dance for U!

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45583443 作者:ca ...