hdu2222Keywords Search (特里)
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
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.
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.
1
5
she
he
say
shr
her
yasherhs
3
注意:在n个keyword中能够反复。在字符串中,前面出现的keyword后面就不能重加。
#include<stdio.h>
#include<malloc.h>
typedef struct nnn
{
int flag;
struct nnn *next[26];
}node;
node *builde()
{
node *p=(node*)malloc(sizeof(node));
p->flag=0;
for(int i=0;i<26; i++)
p->next[i]=NULL;
return p;
}
node *root;
void insert(char str[])
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
if(p->next[str[i]-'a']==NULL)
p->next[str[i]-'a']=builde();
p=p->next[str[i]-'a'];
}
p->flag++;
}
int count(char str[])
{
int sum=0;
for(int i=0;str[i]!='\0';i++)
{
node *p=root;
for(int j=i;str[j]!='\0';j++)
{
if(p->next[str[j]-'a']==NULL)
break;
p=p->next[str[j]-'a'];
sum+=p->flag; p->flag=0;
}
}
return sum;
}
char str[1000005];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
root=builde();
while(n--)
{
scanf("%s",str);
insert(str);
}
scanf("%s",str);
printf("%d\n",count(str));
}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
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呈现 ...
随机推荐
- Swift - iCloud存储介绍
对于开发者而言,涉及iCloud存储的功能主要有两个: 一是 iCloud documnet storage,利用 iCloud 存储用户文件,比如保存一些用户在使用应用时生成的文件以及数据库文件等. ...
- 十天学习PHP之第三天
1)按右边的结构:查看改动表结构 2)按右边的浏览:查看表中的数据 3)按右边的SQL:执行SQL语句 4)按右边的插入:插入一行记录 5)按右边的清空:删除表中全部记录 6)按右边的删除: ...
- 微信支付[v3]
原文:微信支付[v3] V2升级V3 顺便记录一下 ,文档: http://pay.weixin.qq.com/wiki/doc/api/index.html !!! 支付授权目录与测试人的微信帐号白 ...
- 基于visual Studio2013解决面试题之0306打印第一次只出现一次的字符
题目
- 头文件 boost/cstdint.hpp
Header boost/cstdint.hpp 头文件 boost/cstdint.hpp 头文件 <boost/cstdint.hpp> 提供了用于编写要求指定整数宽度的可移植代码的 ...
- CSS中常见的BUG调试
1.布局--layout 布局是windows提出的概念,用于控制元素的尺寸和定位. 拥有布局的元素负责自身及其子元素的尺寸及定位,而没有布局的元素仅仅能依靠近期的祖先元素进行控制. 通常在IE6中出 ...
- MSYS2 环境搭建(在Qt Creator可以设置环境变量来进行引用这些库)
本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...
- 【Visual C++】Windows GDI贴图闪烁解决方法
一般的windows 复杂的界面需要使用多层窗口而且要用贴图来美化,所以不可避免在窗口移动或者改变大小的时候出现闪烁. 先来谈谈闪烁产生的原因 原因一:如果熟悉显卡原理的话,调用GDI函数向屏幕输出的 ...
- Shell 传递参数
Shell 传递参数 向脚本传递参数,格式为:$n. 向脚本传递三个参数,并分别输出: echo "Shell 传递参数实例!"; echo "第一个参数为:$1&quo ...
- iOS 5 故事板入门(3)
原文: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-2 Segues 介绍 是时候在我们的故事板中加入更 ...