这题数据太水,一开始没有加上Get的方法也能AC。。话说AC自动机中一定要注意加上Get的方法!(不然,同一个后缀的其他单词就没被算上了。)

  代码如下:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <iostream>
using namespace std;
const int MAX_N = + ;
const int MAX_Tot = * + ; int ans[+];
map<int,string> M; struct Aho
{
struct state
{
int nxt[];
int fail,cnt;
}stateTable[MAX_Tot]; int size; queue<int> que; void init()
{
while(que.size()) que.pop();
for(int i=;i<MAX_Tot;i++)
{
memset(stateTable[i].nxt,,sizeof(stateTable[i].nxt));
stateTable[i].fail = stateTable[i].cnt = ;
}
size = ;
} void insert(char *s,int which)
{
int n = strlen(s);
int now = ;
for(int i=;i<n;i++)
{
char c = s[i];
if(!stateTable[now].nxt[c-'A'])
stateTable[now].nxt[c-'A'] = size++;
now = stateTable[now].nxt[c-'A'];
}
stateTable[now].cnt = which;
} void build()
{
stateTable[].fail = -;
que.push(); while(que.size())
{
int u = que.front();que.pop();
for(int i=;i<;i++)
{
if(stateTable[u].nxt[i])
{
if(u == ) stateTable[stateTable[u].nxt[i]].fail = ;
else
{
int v = stateTable[u].fail;
while(v != -)
{
if(stateTable[v].nxt[i])
{
stateTable[stateTable[u].nxt[i]].fail = stateTable[v].nxt[i];
break;
}
v = stateTable[v].fail;
}
if(v == -) stateTable[stateTable[u].nxt[i]].fail = ;
}
que.push(stateTable[u].nxt[i]);
}
}
}
} void Get(int u)
{
while(u)
{
ans[stateTable[u].cnt] ++;
u = stateTable[u].fail;
}
} void match(char *s)
{
int n = strlen(s);
int now = ;
for(int i=;i<n;i++)
{
char c = s[i];
if(c<'A' || c>'Z') {now = ;continue;}
if(stateTable[now].nxt[c-'A']) now = stateTable[now].nxt[c-'A'];
else
{
int p = stateTable[now].fail;
while(p != - && stateTable[p].nxt[c-'A'] == ) p = stateTable[p].fail;
if(p == -) now = ;
else now = stateTable[p].nxt[c-'A'];
}
if(stateTable[now].cnt)
{
Get(now);
//ans[stateTable[now].cnt] ++;
/*
一定要用Get这样的方法,
不然,同一个后缀的无法被算上了
*/
}
}
}
}aho; int n;
char s[MAX_N]; int main()
{
while(scanf("%d",&n)==)
{
memset(ans,,sizeof(ans));
M.clear();
aho.init();
for(int i=;i<=n;i++)
{
scanf("%s",s);
M[i] = s;
aho.insert(s,i);
}
aho.build();
scanf("%s",s);
aho.match(s);
for(int i=;i<=n;i++)
{
if(ans[i])
{
cout << M[i] << ": " << ans[i] << endl;
}
}
}
}

————————————————————————————————————————————

  发现上面的代码有问题(虽然能AC),正确代码如下:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <iostream>
using namespace std;
const int MAX_N = + ;
const int MAX_Tot = * + ; int ans[+];
map<int,string> M; struct Aho
{
struct state
{
int nxt[];
int fail,cnt;
}stateTable[MAX_Tot]; int size; queue<int> que; void init()
{
while(que.size()) que.pop();
for(int i=;i<MAX_Tot;i++)
{
memset(stateTable[i].nxt,,sizeof(stateTable[i].nxt));
stateTable[i].fail = stateTable[i].cnt = ;
}
size = ;
} void insert(char *s,int which)
{
int n = strlen(s);
int now = ;
for(int i=;i<n;i++)
{
char c = s[i];
if(!stateTable[now].nxt[c-'A'])
stateTable[now].nxt[c-'A'] = size++;
now = stateTable[now].nxt[c-'A'];
}
stateTable[now].cnt = which;
} void build()
{
stateTable[].fail = -;
que.push(); while(que.size())
{
int u = que.front();que.pop();
for(int i=;i<;i++)
{
if(stateTable[u].nxt[i])
{
if(u == ) stateTable[stateTable[u].nxt[i]].fail = ;
else
{
int v = stateTable[u].fail;
while(v != -)
{
if(stateTable[v].nxt[i])
{
stateTable[stateTable[u].nxt[i]].fail = stateTable[v].nxt[i];
break;
}
v = stateTable[v].fail;
}
if(v == -) stateTable[stateTable[u].nxt[i]].fail = ;
}
que.push(stateTable[u].nxt[i]);
}
}
}
} void Get(int u)
{
while(u)
{
ans[stateTable[u].cnt] ++;
u = stateTable[u].fail;
}
} void match(char *s)
{
int n = strlen(s);
int now = ;
for(int i=;i<n;i++)
{
char c = s[i];
if(c<'A' || c>'Z') {now = ;continue;}
if(stateTable[now].nxt[c-'A']) now = stateTable[now].nxt[c-'A'];
else
{
int p = stateTable[now].fail;
while(p != - && stateTable[p].nxt[c-'A'] == ) p = stateTable[p].fail;
if(p == -) now = ;
else now = stateTable[p].nxt[c-'A'];
}
//if(stateTable[now].cnt)
{
Get(now);
}
}
}
}aho; int n;
char s[MAX_N]; int main()
{
while(scanf("%d",&n)==)
{
memset(ans,,sizeof(ans));
M.clear();
aho.init();
for(int i=;i<=n;i++)
{
scanf("%s",s);
M[i] = s;
aho.insert(s,i);
}
aho.build();
scanf("%s",s);
aho.match(s);
for(int i=;i<=n;i++)
{
if(ans[i])
{
cout << M[i] << ": " << ans[i] << endl;
}
}
}
}
/*
3
ABCDEFG
ABCDE
DEF
ABCDEFG
*/

HDU 3065 病毒侵袭持续中(AC自动机)的更多相关文章

  1. HDU 3065 病毒侵袭持续中 (AC自动机)

    题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...

  2. hdoj 3065 病毒侵袭持续中(AC自动机)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...

  3. HDU 3065 病毒侵袭持续中

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

  4. hdu 3065 病毒侵袭持续中【AC自动机】

    <题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...

  5. HDU 3065 病毒侵袭持续中(AC自己主动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3065 Problem Description 小t非常感谢大家帮忙攻克了他的上一个问题.然而病毒侵袭 ...

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

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

  7. hdu----(3065)病毒侵袭持续中(AC自动机)

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

  8. hdu 3065病毒侵袭持续中

    病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. HDU 3065 病毒侵袭持续中 (模板题)

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

  10. HDU3065 病毒侵袭持续中 —— AC自动机

    题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

随机推荐

  1. [读书笔记]OSGI-灵活的类加载器架构

    以下内容来自周志明的<深入理解Java虚拟机>. 学习JEE规范,去看JBoss源码:学习类加载器,就去看OSGI源码. OSGI,即Open Service Gateway Initia ...

  2. xmind的第八天笔记

  3. java——XML与java对象装换

    -------------------------------Dog类--------------- publicclass Dog implements Serializable{ privates ...

  4. 装饰模式(Decorate Pattern)

    在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. (1) 装饰对象和真实对象有相同的接口.这样客户端对象就能以和真实对象相同的方式 ...

  5. selenium下拉框选择

    下拉框结构如下,我需要选择的是new: html为: <select id="condition_type" name="condition_type" ...

  6. jquery选择器之表单选择\表单对象属性

    :input 匹配所有input标签 :text 匹配所有单行文本框 :password 匹配所有密码框 :radio  匹配所有单选扭 :checkbox 匹配所有复选框 :image 匹配所有图像 ...

  7. SQL2000的三种“故障还原模型”

    一.SQL2000的三种“故障还原模型” 在数据库属性的“选项”页,“故障还原模型”栏,共有三项选择:简单.完全.大容量日志记录.它们的根本差别在于SQL2000对数据库日志的维护方式不同.下面逐个讲 ...

  8. HA(High available)--Heartbeat高可用性集群(双机热备)菜鸟入门级

    HA(High available)--Heartbeat高可用性集群(双机热备)   1.理解:两台服务器A和B ,当A提供服务,B闲置待命,当A服务宕机,会自动切换至B机器继续提供服务.当主机恢复 ...

  9. fgets读取文件时的注意事项

    1 文本文件 a.txt 内容如下 2 c代码 FILE *fil; if (!(fil = fopen("/home/rudy/projects/paser/a.txt", &q ...

  10. 115、定时器(TimerTask+Timer+Handler)

    public class TimerUtils { public static Activity act; public static List<MaiDianModels> listMa ...