题目链接:【http://acm.hdu.edu.cn/showproblem.php?pid=2222】

题意:给出很多小字符串,然后给出一个文本串,问文本串中包含多少个小字符串。也就是说如果文本串中出现了多次某个小字符串,则只算一次。

题解:裸的AC自动机,首先利用小字符串建立一个Trie,然后再Trie上做KMP,使得为一个节点具有一个fail指针,其含义是,当文本串在某个节点失配时,则在Trie上找到根节点到这个节点的最长后缀的一个串,然后指向它。

不断地匹配下去就行了。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
struct Aho_C
{
int next[maxn][26], fail[maxn], End[maxn];
int root, L;
int newnode()
{
for(int i = 0; i < 26; i++)
next[L][i] = -1;
End[L++] = 0;
return L - 1;
}
void init()
{
L = 0;
root = newnode();
}
void Insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
if(next[now][buf[i] - 'a'] == -1)
next[now][buf[i] - 'a'] = newnode();
now = next[now][buf[i] - 'a'];
}
End[now]++;
}
void Build()
{
queue<int>Q;
fail[root] = root;
for(int i = 0; i < 26; i++)
if(next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while( !Q.empty() )
{
int now = Q.front();
Q.pop();
for(int i = 0; i < 26; i++)
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int Query(char buf[])
{
int len = strlen(buf);
int now = root, res = 0;
for(int i = 0; i < len; i++)
{
now = next[now][buf[i] - 'a'];
int temp = now;
while( temp != root )
{
res += End[temp];
End[temp] = 0;
temp = fail[temp];
}
}
return res;
}
}ac;
char buf[maxn * 2];
int main()
{
int T, n;
scanf("%d", &T);
while( T-- )
{
scanf("%d", &n);
ac.init();
for(int i = 0; i < n; i++)
{
scanf("%s", buf);
ac.Insert(buf);
}
ac.Build();
scanf("%s", buf);
printf("%d\n", ac.Query(buf));
}
return 0;
}

  

HDU 2222 Keywords Search 【AC自动机】的更多相关文章

  1. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  2. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  3. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  4. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  5. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  6. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

随机推荐

  1. js和jquery中的遍历对象和数组(forEach,map,each)

    arr[].forEach(function(value,index,array){ //do something }) 参数:value数组中的当前项,index当前项的索引,array原始数组: ...

  2. 【CodeForces】704 C. Black Widow 动态规划+模拟

    [题目]C. Black Widow [题意]给定一个表达式,形式为(...)^(...)^......^(...)=1(n个括号),括号中为1~2个值取或.有m个变量,给出表达式的值为xi或 !xi ...

  3. 【转载】iPhone系统概览

    iPhone OS OverviewiPhone系统概览iPhone OS comprises the operating system and technologies that you use t ...

  4. NB二人组(二)----归并排序

    归并排序的思路: 归并算法程序(配合下图进行思考): def merge(li,low,mid,high): i = low j = mid + 1 ltmp=[] while i <= mid ...

  5. jQuery()方法的第二个参数详解

    关于jQuery()方法的第二个参数,有下面这几种用法: 1.jQuery(selector, [context]) 这种用法,相当于 $(context).find(selector) 或者 con ...

  6. Ubuntu 14.04 开机手动开启numlock led

    在windows开机后,数字键盘灯是亮着的,但是当切换到ubuntu系统后登录用户名和密码时,如果你设定的有数字,都要先打开数字键盘区NUMLOCK键,然后再输入了,很不方便. 首先,需要安装一个小软 ...

  7. 64_p2

    perl-Class-XSAccessor-1.19-10.fc26.x86_64.rpm 12-Feb-2017 15:12 48098 perl-Clipboard-0.13-16.fc26.no ...

  8. MySQL sleep过多解决方法

    睡眠连接过多,会对mysql服务器造成什么影响? 严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃. 造成睡眠连接过多的原因? 1. 使用了太多持久连接(个人觉得,在高并 ...

  9. sicily 1193. Up the Stairs

    Time Limit: 1sec    Memory Limit:32MB  Description John is moving to the penthouse of a tall sky-scr ...

  10. 增加Android模拟器空间(Internal Storage)

    转载 http://vase.iteye.com/blog/2114664   初学Android,发现模拟器上有不少限制,譬如标题中的存储限制,无论用ADT Manager如何设置,内部存储空间不会 ...