hdu2222Keywords Search
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue> using namespace std; struct node
{
node* next[];
int end;
node* fail;
node()
{
for(int i = ;i<;i++)
next[i] = NULL;
fail = NULL;
end = ;
}
};
node* root;
char s[],ss[];
void insert()
{
int i,l = strlen(s);
node* k = root;
for(i = ;i<l;i++)
{
int id = s[i]-'a';
if(k->next[id] == NULL)
k->next[id] = new node();
k = k->next[id];
}
k->end++;
}
void build()
{
queue<node*> q;
for(int i = ;i<;i++)
{
node* k = root;
if(k->next[i] != NULL)
{
k->next[i]->fail = root;
q.push(k->next[i]);
}
}
while(!q.empty())
{
node*k = q.front();
q.pop();
for(int i = ;i<;i++)
{
if(k->next[i]!=NULL)
{
node*t = k->fail;
while(t!=root&&t->next[i] == NULL) t = t->fail;
if(t->next[i] != NULL) t = t->next[i];
k->next[i]->fail = t;
q.push(k->next[i]);
}
}
}
}
int ask()
{
int i,l = strlen(ss),ans = ;
node *k = root;
for(i = ;i<l;i++)
{
int id = ss[i]-'a';
while(k!=root&&k->next[id] == NULL) k = k->fail;
if(k->next[id]!=NULL) k = k->next[id];
node* t = k;
while(t!=root)
{
ans += t->end;
t->end = ;
t = t->fail;
}
}
return ans;
} int main()
{
int z;
int n,i,j,k;
cin>>z;
while(z--)
{
root = new node();
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
insert();
}
build();
scanf("%s",ss);
printf("%d\n",ask());
}
return ;
}
hdu2222Keywords Search的更多相关文章
- hdu2222Keywords Search (特里)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- 【hdu2222-Keywords Search】AC自动机基础裸题
http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...
- hdu2222Keywords Search字典树入门……
#include<iostream> #include<cstring> using namespace std; struct node { int num; node *n ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
- HDU2222Keywords Search AC_自动机
http://blog.csdn.net/niushuai666/article/details/7002823 #include <iostream> #include <cstd ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 基于WebGL 的3D呈现A* Search Algorithm
http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...
随机推荐
- winform —— 界面
winform 界面简单介绍 窗体事件:理解为委托变量,指向哪个函数,就执行哪个函数.窗体:显示窗体的过程是一个通过模板造对象的过程.先走构造函数,构造函数中的InitializeComponent( ...
- Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...
- MyEclipse开发的java web项目在 Eclipse中无法识别
不能识别项目解决办法 在eclipse下,右键项目properties -> project fac e ts 选中 Dynamic web module 选择后面的版本为 2.5(运行环 ...
- HDU 1151 - Air Raid
很明显求最小路径覆盖 就是求最大匹配 #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- 移动端zepto.js文件的构建
因为在zepto的官网http://www.zeptojs.cn/#download下载的文件只包括了默认的几个模块,这并不能全部适应我们所需功 能,还需要按自己需要去添加相应模块.所以需要去构建我们 ...
- Oracle 表连接方式分析 .
一 引言 数据仓库技术是目前已知的比较成熟和被广泛采用的解决方案,用于整和电信运营企业内部所有分散的原始业务数据,并通过便捷有效的数据访问手段,可以支持企业内部不同部门,不同需求,不同层次的用户随时获 ...
- HTML5的结构学习(1) --- 新增的主体结构元素
1.article 元素 解释:代表文档.页面和应用程序中独立的.完整的.可以被独自引用的内容. 主要用途:博客中的文章.评论,贴吧中的帖子,或者独立的插件等. article中可以包含多种元素例 ...
- 使用pymysql连接MySql数据库
MySQLdb安装失败了,直接使用pymysql,安装了pymysql. 并学习了使用使用pymysql创建数据库和表,并插入数据. __author__ = 'Administrator' impo ...
- win32 调用多媒体函数PlaySound()
必须引入此头文件 #include <mmsystem.h>#pragma comment(lib, "WINMM.LIB") /*------------------ ...
- python笔记之Cmd模块
python笔记之Cmd模块 Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作.命令行编辑和命令完成. 使用cmd创建的命令行解释器循环读取输入的所有行并 ...