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. 加固linux

    http://linoxide.com/linux-command/password-aging-secure-linux-access/

  2. NSNotificationCenter消息通信(KVO)

    NSNotificationCenter是程序不同类间的消息通信. 注册消息通知: [[NSNotificationCenter defaultCenter]addObserver:self sele ...

  3. cpu信息

    在linux系统下能够通过cat /proc/cpuinfo来查看本机上cpu的相关信息,通过processor能够推断逻辑cpu的个数,physical id能够推断物理cpu的个数,通过cpu c ...

  4. Source Insight 3.50.0065使用详解

    转自calvinlee1984 Subject:Source Insight3.50.0065使用详解 Date:     21-Oct-2011 By:         Calvinlee1984@ ...

  5. 【POJ 1226】Substrings

    [链接]h在这里写链接 [题意] 给你n个字符串. 让你找一个字符串s. 设s'为这个字符串的逆序. 要求s或者s'在每个字符串里都能够找得到. 并且要求s的长度最长. 求出这个最长的串的长度. [题 ...

  6. swift开发网络篇 - post 请求

    /** 所有网络请求,统一使用异步请求! 在今后的开发中,如果使用简单的get/head请求,可以用NSURLConnction异步方法 GET查/POST增/PUT改/DELETE删/HEAD GE ...

  7. GDB如何调试没有符号表(未加-g选项的编译)的程序

    /********************************************************************* * Author  : Samson * Date    ...

  8. 35、在JZ2440上使用3G上网卡

    1. 简单使用:1.1 选型:中国联通:E网时空 EW65 (64元), ZTE中兴 MF637U (160多)中国电信:Benton/本腾 EQ10B (35元)中国移动:华为 ET128 (99元 ...

  9. 0、驱动及应用小技巧、uboot指令及环境变量配置、linux常用命令

    (内核make menuconfig之后,通过insmod安装的驱动都应该重新make,可能会出现一些莫名的问题) (nor flash/SDRAM/DM9000都受内存控制器控制,需要配置内存控制器 ...

  10. java导出word直接下载

    导出word工具类 package util; import java.io.IOException; import java.io.Writer; import java.util.Map; imp ...