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呈现 ...
随机推荐
- cocos2d-x在win32和iOS、android下获取当前系统时间的方法
最近在游戏里要显示当前系统时间的功能,网上一搜很多写着获取的方法,大都是如下 struct cc_timeval now; CCTime::gettimeofdayCocos2d(&now, ...
- commondatastorage.googleapis.com訪问失败高速解决
谷歌更新以后非常多sampleproject下载不了. http://commondatastorage.googleapis.com訪问失败高速解决这个问题. 使用在线代理就可以,随便推荐一个htt ...
- 让程序在崩溃时体面的退出之Dump文件
在我的那篇<让程序在崩溃时体面的退出之CallStack>中提供了一个在程序崩溃时得到CallStack的方法.但是要想得到CallStack,必须有pdb文件的支持.但 ...
- Python中的继承
继承: 面向对象程序语言的一个重要特点是继承.继承提供了在已存在类的基础上创建新类的方法.继承的子类 拥有被继承的父类的所有方法,在此基础上,子类还可以添加自己的专有方法.继承是类的强有力的特点.一些 ...
- android中保存Bitmap图片到指定文件夹中的方法
/** 保存方法 */ public void saveBitmap() { Log.e(TAG, "保存图片"); File f = new File("/s ...
- WinExec函数,启动其他应用程序
WinExec The WinExec function runs the specified application. Note This function is provided only fo ...
- Android四大组件--Broadcast Receiver具体解释
本文主要讲述了: 一.BroadcastReceiver概述: 二.BroadcastReceiver事件分类 三.BroadcastReceiver事件的编程流程 四.两类BroadcastRece ...
- 【从cocos2d-x学习设计模式】第一阶段:辛格尔顿
设计模式,它总结了前辈在许多方案重用代码.它是一个想法. 因为我们爱cocos2d-x,然后我们从去cocos2d-x在设计模式中,右一起学习!本篇解释未来辛格尔顿. 提cocos2d-x中间Dire ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- haproxy 看到的是https,后台是http的原因
https://www.zjtest6.com/admin/api/menu haproxy 日志: Jun 24 13:23:02 localhost haproxy[23205]: 192.168 ...