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 ...
随机推荐
- 数学函数类方法的使用.java
public class Test { public static void main(String[] args) { double a=2,b=3; double z1=Math.pow(a,b) ...
- PHP二维数组排序函数
PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义. 以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义: functi ...
- canvas加载图像
之前按照例子,加载图像,发现加载图像,加载不上去,代码也没有错误,经过几次的尝试:发现需要重复调用下drawImage才可以. <script type="text/javascrip ...
- IOC容器MEF在MVC中的使用
最近想把自己的网站框架用IOC改造下,经过对比,我初步选择autofac,虽然MEF不需要配置,但性能不行,autofac虽然需要自己写自动化注入,但性能非常好. 先分析下各大IOC框架的性能,分两类 ...
- Hibernate学习笔记-Hibernate关系映射
1. 初识Hibernate——关系映射 http://blog.csdn.net/laner0515/article/details/12905711 2. Hibernate 笔记8 关系映射1( ...
- BeanUtils框架浅析
一.使用步骤: 1.添加jar包: commons-beanutils-1.8.0.jar commons-logging.jar 2.使用setProperty()方法对javabean设置属性值 ...
- cf E. Dima and Magic Guitar
http://codeforces.com/contest/366/problem/E |x1-x2|+|y1-y2|有四种情况 1.-(x1-x2)+(y1-y2); 2.(x1-x2)-(y1-y ...
- PlayerPrefs类
该类用于本地持久化保存与读取数据工作原理是:以键值对的形势将数据保存在文件中.该类可以保存与读取3种基本的数据类型,它们是浮点型.整型和字符串型,涉及的方法如下.SetFloat():保存浮点类型Se ...
- 关于iostream的效率问题
前言 经常有人说iostream的速度慢,IO流比stdio的慢多了.但是有人测试过的,iostream的速度是超过stdio的. 测试结果 /* C */#include <stdio. ...
- Smarty 保留变量
{$smarty} 保留变量 可以通过PHP的保留变量 {$smarty}来访问一些环境变量. 下面是这些变量的列表: 页面请求变量 页面请求变量如$_GET, $_POST, $_COOKIE, $ ...