然而还不是很懂=_=

 #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. Shell: test

    基本格式:test expression expression为test命令构造的表达式.这里expression是test命令可以理解的任何有效表达式,该简化格式将是读者可能会踫见的最常用格式返回值 ...

  2. com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Cannot assign requested address: bind

    在发布 web service 时报错: Endpoint.publish(publishAddress, hl7MessageReveiver); com.sun.xml.internal.ws.s ...

  3. 大数据系列(4)——Hadoop集群VSFTP和SecureCRT安装配置

    前言 经过前三篇文章的介绍,已经通过VMware安装了Hadoop的集群环境,当然,我相信安装的过程肯定遇到或多或少的问题,这些都需要自己解决,解决的过程就是学习的过程,本篇的来介绍几个Hadoop环 ...

  4. Python进阶【第一篇】socket

    1.socket模块 要使用socket.socket()函数来创建套接字.其语法如下: socket.socket(socket_family,socket_type,protocol=0) soc ...

  5. lmap

    1.lamp组件安装 sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install mysql-server ...

  6. Linux & Systemd 挂载问题解决

    1. 方法一: Create the following file as root: /etc/polkit-/rules.d/-nopasswd_global.rules /* Allow memb ...

  7. JS -- 异步加载进度条

    今天在博客园问答里面看到博友问道怎么实现Ajax异步加载产生进度条. 很好奇就自己写了一个. 展现效果: 1) 当点击Load的时候,模拟执行异步加载. 浏览器被遮挡. 进度条出现. 实现思路: 1. ...

  8. 一个简单的统计图像主颜色的算法(C#源代码)

    前段日子有朋友咨询了下分析图像主颜色的算法,我对这一块也没有什么深入的研究,参考了一些小代码,然后自己写了一个很简单的小工具,现共享给大家. 界面截图如下: 算法的原理很简单,就是统计出图像中各种颜色 ...

  9. 虚基类&虚继承

    发现这个月准备竞赛完全没有更新哎... 改了下某华大一c++测试题...网上对虚继承讲的要么太繁琐要么不到位,自力更生 #include<iostream> #include<fst ...

  10. Tomcat7服务器删除后重新安装失败问题

    学习JSP时,需要修改conf下的配置文件,确发现无论如何修改权限都无法保存,所以决定卸载重装到D盘,卸载时又弹出一个对话框,具体内容没看,但是就是卸载不了,所以打算手动删除. 删除后,重装时安装失败 ...