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呈现 ...
随机推荐
- PB+MS SQL+触发器必须指出
PB+MS SQL+触发器必须指出: 若触发器存在两笔以上的返回值,比方两条update 语句,被误判为数据有改变.存盘不成功. 提示: Row changed between retrieve an ...
- openstack中Nova组件images的全部python API 汇总
感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- 一个关于native sql的程序
*&---------------------------------------------------------------------* *& Report ZHR_BPM11 ...
- Centos系统各种日志存详解
Centos系统各种日志存储路径和详细介绍 Linux常见的日志文件详述如下 1./var/log/boot.log(自检过程) 2./var/log/cron (crontab守护进程crond所派 ...
- OSGi:生命周期层
前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或者两者的结合),并且给了应用本 ...
- struts2由<s:tree>要么dtree小工具 建立树
一个 .<s:tree>方法: 1.引入新的标签: <%@ taglib prefix="sd" uri="/struts-dojo-tags" ...
- jquery实现鼠标焦点十字效果
系统开发时很多地方需要有焦点效果,例如:鼠标点击聚焦,地图定位,在图片上突出显示,焦点定位页面元素. 本小功能通过jquery和graphics二次开发,实现通过鼠标点击页面任何区域,聚焦当前点击位置 ...
- 积累的VC编程小技巧之标题栏和菜单
1.窗口最大最小化按纽的控制 ①怎样在程序开始的时候让它最大化? ②vc++做出来的exe文件在窗体的右上方是没有最大化和最小化按钮的,怎样实现这一功能? ③如何在显示窗口时,使最大化按钮变灰? ...
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
//from http://kingron.myetang.com/zsfunc0d.htm (*// 标题:充分利用pascal字符串类型 说明:和PChar不同,string可以保存# ...
- expect 参数处理之一
expect 参数处理之一 expect是很强悍的脚本,网上资源虽然很经典,但很少,我把自己的测试脚本贴之,仅作参考 #!/usr/bin/expect -f proc help {} { puts ...