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. .NET中的CSV导入导出(实例)

    导入代码,从csv文件得到datatable         /// <summary>        /// Get Data From Csv File         /// (Th ...

  2. 疯狂Android第一章:Android环境配置以及基本概念

    第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...

  3. 2014年2月5日 Oracle ORACLE的工作机制[转]

      网上看到一篇描写ORACLE工作机制的文章,觉得很不错!特摘录了下来.   ORACLE的工作机制-1 (by xyf_tck) 我们从一个用户请求开始讲,ORACLE的简要的工作机制是怎样的,首 ...

  4. WCF如何在浏览器访问

    1.新建wcf服务看到有如下两个方法,在浏览器调用框中的方法.

  5. UVALive 6959 - Judging Troubles

    给两组字符串,最多有多少对相同. map做映射判断一下. #include <iostream> #include <cstdio> #include <map> ...

  6. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  7. 认识Java里面的Thread

    在一个特定的主线程执行的过程中,如果我们还需要在主线程的过程中插播一个线程,做其他动作.那么我们就可以利用Java的Thread类,创建一个新的线程. 一:线程简单实现的三种方式 (1)第一种创建线程 ...

  8. [Oracle]查看和修改连接数

    #登陆数据库sqlplus system/*** as sysdba #显示当前最大连接数:show parameter processes; show parameter sessions; #修改 ...

  9. JS三元运算符

    语法是 条件 ? 结果1 : 结果2; 问号(?)的前面是条件:后面跟着用冒号(:)分隔的结果1和结果2;满足条件时结果1否则结果2. 例子: var a=1; a>2?a:a=3; alert ...

  10. Exception和IOException之间的使用区别

    Exception和IOException之间的使用区别 先看一段代码.这段代码来自<深入剖析tomcat>   public void await() { // 创建ServerSock ...