然而还不是很懂=_=

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int Max = +;
char str[Max];
struct node
{
int cnt;
struct node * Next[];
struct node * fail;
void init()
{
for (int i = ; i < ; i++)
Next[i] = NULL;
cnt = ;
fail = NULL;
}
};
node * root;
void Insert()
{
node * p = root;
int len = strlen(str);
for (int i = ; i < len; i++)
{
int id = str[i] - 'a';
if (p->Next[id] == NULL)
{
p->Next[id] = new node();
p->Next[id]->init();
}
p = p->Next[id];
}
p->cnt++;
}
void getFail()
{
node * p = root, *temp, *son;
queue<struct node * > que;
que.push(p);
while (!que.empty())
{
temp = que.front();
que.pop();
for (int i = ; i < ; i++)
{
son = temp->Next[i];
if (son != NULL)
{
if (temp == root)
{
son->fail = root;
}
else
{
p = temp->fail;
while (p)
{
if (p->Next[i])
{
son->fail = p->Next[i];
break;
}
p = p->fail;
}
if (!p)
son->fail = root;
}
que.push(son);
}
}
}
}
void querry()
{
int len, cnt = ;
len = strlen(str);
node * p, * temp;
p = root;
for (int i = ; i < len; i++)
{
int pos = str[i] - 'a';
while (!p->Next[pos] && p != root)
p = p->fail;
p = p->Next[pos];
if (!p)
p = root;
temp = p;
while (temp != root)
{
if (temp->cnt >= )
{
cnt += temp->cnt;
temp->cnt = -;
}
else
break;
temp = temp->fail;
}
}
printf("%d\n", cnt);
}
int main()
{
int test, n;
scanf("%d", &test);
while (test--)
{
root = new node();
root->init();
root->fail = NULL;
scanf("%d", &n);
getchar();
for (int i = ; i < n; i++)
{
gets(str);
Insert();
}
getFail();
gets(str);
querry();
}
return ;
}

HD2222 Keywords Search(AC自动机入门题)的更多相关文章

  1. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  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自动机)(模板题)

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

  5. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  6. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  7. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  9. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. Oracle 12.1.0.2 New Feature翻译学习【In-Memory column store内存列存储】【原创】

    翻译没有追求信达雅,不是为了学英语翻译,是为了快速了解新特性,如有语义理解错误可以指正.欢迎加微信12735770或QQ12735770探讨oracle技术问题:) In-Memory Column ...

  2. 用tpcc测试对比 innodb 和 tokudb

    测试环境 1台IBM Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz,内存12G cd tpcc/tpcc-mysql/src # make cc lo ...

  3. Kafka Linux 安装

                            要先设置host, etc/hosts,添加     127.0.0.1 机器名         创建目录             修改日志保存位置   ...

  4. linux 搜索相关命令(2)

    文件搜索相关命令 1:locate命令 需要 yum install mlocate locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命 ...

  5. Ubuntu 更新源失败[GPG error]

    对于错误 GPG error: http://ppa.launchpad.net precise Release: The following signatures couldn't be verif ...

  6. 【2016-11-6】【坚持学习】【Day21】【主窗口关闭时,同步关闭它的子窗口】

    本来想用委托实现的.但是又觉得没有必要. 方法如下: public MainWindow() { InitializeComponent(); this.Closing += MainWindow_C ...

  7. [No000074]C#创建桌面快捷方式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. jquery的add()用法总结

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

  9. JavaScript数组:增删改查、排序等

    直接上代码 // 数组应用 var peoples = ["Jack","Tom","William","Tod",&q ...

  10. JS中判断null、undefined与NaN的方法

    写了个 str ="s"++; 然后出现Nan,找了一会. 收集资料如下判断: 1.判断undefined: 代码如下: <span style="font-siz ...