Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched. To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 
Input
First line will contain one integer means how many cases will follow by. Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000) Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50. The last line is the description, and the length will be not longer than 1000000.
 
Output
Print how many keywords are contained in the description.
 
Sample Input
1 5 she he say shr her yasherhs
 
Sample Output
3
 
Author
Wiskey

#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的更多相关文章

  1. hdu2222Keywords Search (特里)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  2. 【hdu2222-Keywords Search】AC自动机基础裸题

    http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...

  3. hdu2222Keywords Search字典树入门……

    #include<iostream> #include<cstring> using namespace std; struct node { int num; node *n ...

  4. hdu2222--Keywords Search+AC自己主动机模板

    题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...

  5. HDU2222Keywords Search AC_自动机

    http://blog.csdn.net/niushuai666/article/details/7002823 #include <iostream> #include <cstd ...

  6. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. 基于WebGL 的3D呈现A* Search Algorithm

    http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...

随机推荐

  1. $(document).ready()与window.onload的区别(转发)

    1.执行时间window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.$(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕.2.编写个数不同wind ...

  2. S - stl 的mapⅠ

    先来介绍一下stl中的map这个功能 头文件#include<map> map是STL的一个关联容器,它提供一对一的数据处理能力 就像一个人对应一个编号一样 定义 为  map<in ...

  3. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  4. IE的documentMode属性

    参看下面链接:<IE的documentModeshuxing>

  5. Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]

    项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...

  6. Oracle EBS-SQL (CST-2):检查有BOM但成本不基于累积的数据.sql

    select c.segment1                                                                          物料编码,     ...

  7. Memory Leak(内存泄漏)问题总结(转)

    最近听了一些关于Memory Leak(内存泄漏)的seminar,感觉有些收获,所以留个记录,并share给朋友. 1 什么是Memory Leak. Memory Leak是指由于错误或不完备的代 ...

  8. linux命令之ls命令的简明讲解

    我详细熟悉linux操作系统的同学对ls命令是再熟悉不过了因为我们通常使用该命令查看某个目录下的文件 其命令形式是:ls [选项] 目录或者文件 本文中主要讲解两个最常用的选项: -l:用于列出文件的 ...

  9. 网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【2】- 在线编程(难度:中)

    第6单元作业[2]- 在线编程(难度:中) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...

  10. 无组件客户端js图片压缩

    <div class="free-upload"> <p>上传您的约会照片,一张合影.一张票据哦!</p> <div class=&quo ...