hdu Repositoryti
算法:字典树
题意:给你一些字符串,然后会有一些询问,输出询问在给定的字符串中出现了多少次(字串也是);
例如 add,子串有:a ,d,d,ad ,dd,add;你会发现子串d出现了两次,那么怎么办呢;这就需要
一个标记符号,如果存在有相同的子串就不做处理;
Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository
and some queries, and required to simulate the process.
Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer
Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
Sample Output
0
20
11
11
2
代码:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#define Max 27
using namespace std;
struct dot
{
dot *next[Max];
int flag;
int v;
int num;//用来标记当前是第几个字符串
};
dot *newnode()
{
dot *temp=new dot;
temp->flag=0;
temp->v=0;
for(int i=0;i<Max;i++)
temp->next[i]=0;
return temp;
}
void tree(char *st,int &k,dot *root,int n)
{
dot *p=root;
int id=0;
for(int i=k;i<strlen(st);i++)
{
id=st[i]-'a';
if(p->next[id]==0)
p->next[id]=newnode();
p=p->next[id];
if(p->num!=n)//用来判断是否是同一个字符串;
{p->v++;
p->num=n;
}
}
p->flag=1;
}
int find(char *st,dot *root)
{
dot *p=root;
int id=0;
for(int i=0;i<strlen(st);i++)
{
id=st[i]-'a';
if(p->next[id]==0)
return 0;
p=p->next[id];
}
return p->v;
}
int main()
{
int n,m,i,j,k;
char st[21];
dot *root=new dot;
root=newnode();
cin>>n;
while(n--)
{
cin>>st;
for(k=0;k<strlen(st);k++)
tree(st,k,root,n+1);
}
cin>>m;
while(m--)
{
cin>>st;
cout<<find(st,root)<<endl;
}
return 0;
}
hdu Repositoryti的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- [Mugeda HTML5技术教程之7]添加动画
前一节我们讲述了怎么在新建的作品中添加元素,元素加好以后我们还想让他们动起来,来实现比较炫的效果.这节我们将要讲述怎么给元素添加动画.Mugeda动画是通过时间轴和帧来实现的.通过在时间轴上创建图层和 ...
- PCB正片和负片有什么区别
概念:正片和负片是底片的两种不同类型. 正片:简单地说就是,在底片上看到什么就有什么. 负片:正好相反,看到的就是没有的,看不到的就是有的.见下图: 在 Allegro中使用正负片的特点: 正片:优点 ...
- ANDROID (eclipse)开发常见问题及解决办法汇总
1.ANDROID 在eclipse中没有出现AVD的解决方法(转)如果android安装正确的话,但是eclipse里面的导航条就是没有AVD 可以通过「Window」⇒「Customize Per ...
- 【转】Android中BindService方式使用的理解
原文网址:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html 最近学习了一下Android里面的Service的应 ...
- AS3给显示对象加边框
给显示对象加边框,可以有以下三种方法1.根据相交路径的缠绕规则的奇偶规则法(使用奇偶缠绕规则时,任何相交路径都交替使用开放填充与闭合填充.如果使用同一填充绘制的两个正方形相交,则不会填充相交的区域.通 ...
- EBS服务器管理
EBS 服务的重启 停止Oracle Applications 服务的顺序为:停止应用产品相关服务,再停止数据库相关服务: 启动Oracle Applications 服务的顺序为:启动数据库相关服务 ...
- poj 3692 Kindergarten (最大独立集之逆匹配)
Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and al ...
- hdu 4869 Turn the pokers(组合数+费马小定理)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...
- (转)iOS7界面设计规范(2) - UI基础 - iOS应用解析
今天再来一发,然后结束掉周六的忙碌,去吃零食,还有冰啤酒:其实现在打嗝还有小龙虾味儿呢. 第二篇更多的是从技术的角度对iOS界面组成原理进行了简单的解析,篇幅很短,可稍作了解:更多关于iOS开发入门的 ...
- http接口测试浏览器插件
http接口测试浏览器插件: Chrome: https://chrome.google.com/webstore/detail/chrome-poster/cdjfedloinmbppobahmon ...