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呈现 ...
随机推荐
- 十分钟开发一个调用Activity的PhoneGap插件
在HybridApp开发中,非常多业务我们是没有办法通过HTML5+js实现的,比方调用第三方的包括Activity的jar包,一些必须使用原生代码才干实现的功能,比方复杂的UI的效果,调用通讯相关的 ...
- 通过无线连接的方式来做 Appium 自动化
感谢TesterHome里各种大牛,提出的宝贵思路,我这里只是将他们的想法综合了一下,试出来的成果,谢谢大家分享你们的智慧. 简单说下背景: 由于公司要测试APP 产品的耗电问题,我们采取的办法很lo ...
- JQuery 插件之Ajax Autocomplete(ajax自动完成)
平时用百度,谷歌搜索的时候 会有一个下 拉列表进行提示 这是一个非常好的功能 本文要介绍的这个JQuery 插件 名叫Ajax Autocomplete 顾名思义 ajax 也就是用ajax的方式获取 ...
- ExtJs4 笔记(12) Ext.toolbar.Toolbar 工具栏、Ext.toolbar.Paging 分页栏、Ext.ux.statusbar.StatusBar 状态栏
本篇讲解三个工具栏控件.其中Ext.toolbar.Toolbar可以用来放置一些工具类操控按钮和菜单,Ext.toolbar.Paging专门用来控制数据集的分页展示,Ext.ux.statusba ...
- android:改动PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
1.改动PagerTabStrip中的背景颜色 我们在布局中直接设置background属性就可以: <android.support.v4.view.ViewPager android:id= ...
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
//from http://kingron.myetang.com/zsfunc0d.htm (*// 标题:充分利用pascal字符串类型 说明:和PChar不同,string可以保存# ...
- Qt调用Delphi编写的COM组件
这个问题捣鼓了两天,现在终于解决了,做个笔记分享给大家,以免走弯路 起初,我的想法是在DLL中写一个interface并从函数中导出这个interface,像这样的代码 ICom1 = interfa ...
- tc3162目录
lsbin dev lib proc tmp usrboaroot etc linuxrc sbin userfs var# ...
- 使MYSQL能被外部访问_xeyuu_新浪博客
使MYSQL能被外部访问_xeyuu_新浪博客 使MYSQL能被外部访问 (
- iOS学习笔记(十五)——数据库操作(SQLite)
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的 ...