题目链接:【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. Redis数据类型之列表(list)

    1. 什么是列表 redis的列表使用双向链表实现,往列表中放元素的时候复杂度是O(1),但是随机访问的时候速度就不行了,因为需要先遍历到指定的位置才可以取到元素. 既然列表是使用链表实现的,那么就说 ...

  2. textarea输入框随内容撑开高度

    原文链接 方法一(jquery): $('textarea').each(function () {  this.setAttribute('style', 'height:' + (this.scr ...

  3. Double类型的数向上取整和向下取整

  4. oracle查看表中数据的大小

    通过从视图 user_segments的字段 bytes中找到 select SUM(bytes)/1024/1024 from user_segments where segment_name='E ...

  5. xargs -i 和-I 的区别【转】

    xargs与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如  # find . -type f -name "*.log" | xargs rm -rf * ...

  6. AJP与HTTP比较和分析

    系统环境: OS:Ubuntu 10.10 (2G) Servlet Container:tomcat-tomcat-7.0.23  (最大内存:default 256M  maxThreads:50 ...

  7. 关于boost 的smart_ptr 的使用问题

    boost 的smart_ptr 库中含有好几种智能指针,大家用的最多的应该是shared_ptr ,为啥呢?好用,不用管他啥时候会自动删除等等,而且拷贝和复制都很到位, 但实际上,这个库也有问题,连 ...

  8. rds 与mysql 进行主从同步

    .rds上默认会有server-****,只需要配置从数据库: .从数据库的配置流程: .[mysqld] log-bin = mysql-bin-changelog #要和主库中的名字一样 rela ...

  9. NTP算法

    网络时间协议 由特拉华大学的David L. Mills热心提供.http://www.eecis.udel.edu/~mills mills@udel.edu 由Reinhard v. Hanxle ...

  10. scrapy爬虫

    a. 配置文件 #settings.py DEPTH_LIMIT = 1 #指定“递归”的层数 ROBOTSTXT_OBEY = False #对方网站规定哪些网址可以爬,这个选项表示不遵循此规定 b ...