算法:字典树

题意:给你一些字符串,然后会有一些询问,输出询问在给定的字符串中出现了多少次(字串也是);

例如 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的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. HTML5 Canvas基础知识

    HTML5画布 1.创建一个画布         <canvas id="myCanvas" width="200" height="100&q ...

  2. jquery的queue方法

    queue: queue主要用于给元素上的函数队列(默认名为fx)添加函数(动画效果),这样dequeue就可以取出并执行函数队列中的第一个函数(即最先进入函数队列的函数),delay则可以延迟元素上 ...

  3. 转载:对#!/bin/sh的认识

    转载网址:http://blog.163.com/hashes@yeah/blog/static/16867631220101029847420/ 对#!/bin/sh的认识 第一次学shell编程, ...

  4. PHP扩展开发(3)-config.m4

    1. 宏命令 1.1. dnl 注释      1.2. 扩展的工作方式           1.2.1) PHP_ARG_WITH不需要第三方库           1.2.2) PHP_ARG_E ...

  5. 【转】对ARM堆栈的理解

    对ARM堆栈的理解 堆栈严格来说应该叫做栈,栈(Stack)是限定仅在一端进行插入或删除操作的线性表.因此,对栈来说,可以进行插入或删除操作的一端端称为栈顶(top),相应地,另一端称为栈底(bott ...

  6. Spring AOP 原理

    AOP将应用系统分为两部分,核心业务逻辑(Core businessconcerns)及横向的通用逻辑,也就是所谓的方面Crosscutting enterprise concerns,例如,所有大中 ...

  7. 原生js和jquery实现图片轮播特效

    本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...

  8. 从现在开始使用nodejs开发的几点答疑

    1.为什么要开始用nodejs, 而不是php 理由有三点: 因为我是前端程序员出身,nodejs都是用javascript写的,这样相当于前端和后端都使用javascript,开发更加有效率.当然很 ...

  9. IntelliJ IDEA: maven & jetty 开发 java web

    之前使用eclipse + maven + jetty开发java web应用,本着no zuo no gain的想法, 折腾了一下Intellj idea下开发环境的搭建,顺带学习了maven re ...

  10. 【转】如何调整visio绘图区域尺寸大小

    原文网址:http://jingyan.baidu.com/article/948f5924033870d80ff5f9f1.html 在使用microsoft visio软件绘图时,为了绘图的质量和 ...