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. swfobject.js 2.2简单使用方法

    swfobject.js 2.2简单使用方法 官方网址介绍http://code.google.com/p/swfobject/wiki/documentation 用法:html部分<div ...

  2. 使用FMDB,libqrencode实现二维码的生成并且保存到数据库

    /**  * 1.首先导入第三方库FMDB和libqrencode,添加库libsqlite3.tbd     

  3. CodeForces 501B - Misha and Changing Handles

    有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...

  4. Ubuntu安装中文字体

    Ubuntu没有宋体,楷体之类的中文字体,在Libreoffice中打不出中文,-_-! 我们可以从windows中借点字体过来,哈哈. 一.准备字体 从windows7中拷贝出字体文件,拷贝的目录为 ...

  5. php非递归无限级分类.

    项目需要.递归无限级分类效率实在太低.理了半天思路写的. 分类越多效率越高. /** * 单次循环返回无限极分类嵌套 * @param array $data 操作的数组 * @param strin ...

  6. 开源轻量级移动端友好的JS地图库——leaflet学习教程

    Leaflet 是一个为建设交互性好适用于移动设备地图,而开发的现代的.开源的 JavaScript 库.代码仅有 33 KB,但它具有开发在线地图的大部分功能.Leaflet设计坚持简便.高性能和可 ...

  7. 使用HTML5的页面资源预加载(Link prefetch)功能加速你的页面加载速度

    不管是浏览器的开发者还是普通web应用的开发者,他们都在做一个共同的努力:让Web浏览有更快的速度感觉.有很多已知的技术都可以让你的网站速度变得更快:使用CSS sprites,使用图片优化工具,使用 ...

  8. ng-click得到当前元素,angular.element()用法

    <!DOCTYPE html> <html> <head> <title></title> <script src="lib ...

  9. Android 底部Dialog显示

    public void showComplainDialog() { ComplainDialog complain_dialog = new ComplainDialog(OrderDetialAc ...

  10. Print! Print! Print!

    print语句可以实现打印--只是对程序员友好的标准输出流的接口而已. 从技术角度来讲,这是把一个或多个对象转换为其文本表达形式,然后发送给标准输出或另一个类似文件的流. 更详细地说,在Python中 ...