算法:字典树

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

例如 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. C/C++中的浮点数运算

    代码: #include <iostream> #include <cstdio> #include <cfloat> using namespace std; i ...

  2. MySQL 数据库操作命令汇总

    此文全部都是基本的数据库语言 1.登陆到mysql >mysql -h hostname -u username -p 然后等待系统提示输入密码即可登陆.如果想在登陆的时候就选择好数据库,可以使 ...

  3. DataTables语言国际化

    $('#example').DataTable({     language: {         "sProcessing": "处理中...",       ...

  4. windows上putty访问ubuntu

    1. Ubuntu中安装ssh-server实现远程登录 a) 安装:sudo apt-get install openssh-server b) 开启服务:sudo /etc/init.d/ssh ...

  5. POJ 1225 Substrings

    http://poj.org/problem?id=1226 题意:给定n个串.求一个最长的串,使得这个串或者其反串在每个串中都出现过? 思路:先在大串里面加入正反串,然后二分,判定即可. #incl ...

  6. Qt for Windows - Deployment和它的参数

    http://doc.qt.io/qt-5/windows-deployment.html

  7. PowerShell文件系统(一)前言

    PowerShell文件系统(一)前言 3 12 2月, 2014  在 Powershell tagged Powershell教程 / 别名 / 文件系统 by Mooser Lee PowerS ...

  8. Linux 文件名匹配

    As the shell reads each line, it "handles" any special characters. This includes variable ...

  9. PHP关于时区问题

    最近在学习PHP过程中发现PHP中的格式化时间戳比北京时间晚了8个小时,上网搜索发现原来是时区不对,解决办法是:      1.永久修改           更改php.ini文件中的data.tim ...

  10. javaweb 登录注册

    1:用户登录界面 login.jsp <%@ page language="java" import="java.util.*" pageEncoding ...