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

题解:字典树,把每个字符串的字串都用字典树保存下来,而且记录下每个子串的个数,可是要注意每个字符串可能有同样的字串。此时仅仅加一次。比如ababc,ab仅仅加一次

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; struct Node
{
int id;
int num;
Node* next[26];
Node()
{
id = -1;
num = 0;
for(int i = 0;i < 26;i++)
{
next[i] = NULL;
}
}
}; Node* root; void insert(char* s,int k)
{
int len = strlen(s);
Node* p = root;
for(int i = 0;i < len;i++)
{
if(p->next[s[i] - 'a'] == NULL)
{
Node* q = new Node();
q->id = k;
q->num = 1;
p->next[s[i] - 'a'] = q;
}
p = p->next[s[i] - 'a'];
if(p->id != k)
{
p->id = k; //该字符串中该子字符已经出现了
p->num++; //该字串个数加一
}
}
} int find(char* s)
{
int len = strlen(s);
Node* p = root;
for(int i = 0;i < len;i++)
{
if(p->next[s[i] - 'a'] == NULL)
{
return 0;
}
p = p->next[s[i] - 'a'];
}
return p->num;
} void del(Node*& p)
{
for(int i = 0;i < 26;i++)
{
if(p->next[i] != NULL)
{
del(p->next[i]);
}
}
delete p;
} int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
root = new Node();
char s[100];
for(int i = 0;i < n;i++)
{
scanf("%s",s);
for(int j = 0;s[j] != '\0';j++)
{
insert(s + j,i);
}
}
int q;
scanf("%d",&q);
for(int i = 0;i < q;i++)
{
scanf("%s",s);
printf("%d\n",find(s));
}
del(root);
} return 0;
}

hdu(2846)Repository的更多相关文章

  1. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  2. hdu(1171)多重背包

    hdu(1171) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  4. Spring-data-jpa 笔记(二) Repository 详解

    基础的 Repository 提供了最基本的数据访问功能,其几个子接口则扩展了一些功能.它们的继承关系如下: Repository: 是 spring Data 的一个核心接口,它不提供任何方法,开发 ...

  5. Crisis of HDU(母函数)

    Crisis of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 2111:Saving HDU(贪心)

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

  7. 杭电 2111 Saving HDU (贪心)

    Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的.   一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...

  8. HDU(1285)—确定比赛名次

    /*最近都在复习期末了...好久没做题,都快没智商了*/   有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后 ...

  9. 2-sat入门(tarjan)hdu(3062)

    hdu3062 Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. javascript中的分号【;】

    以前一直以为,在编写js代码的时候,如果在代码后面不添加分号,JavaScript会自动填补分号.最近看了权威指南,才突然发现一直理解有误,而且关于分号的使用,还有很多需要注意的地方. 1.分号的省略 ...

  2. 不用任何插件,实现一个tab栏切换

    //使用jquery中获取当前索引的方法.显示隐藏 <script> $(".tab_list li").on('click', function () { $(thi ...

  3. Java实现一个简单的网络爬虫

    Java实现一个简单的网络爬虫 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWri ...

  4. mysql基础知识点梳理

    ##本单元目标 一.为什么要学习数据库 二.数据库的相关概念DBMS.DB.SQL 三.数据库存储数据的特点 四.初始MySQL MySQL产品的介绍MySQL产品的安装 ★MySQL服务的启动和停止 ...

  5. Css问题 margin float 文档流 背景图底部充满

    今天来整理一下做网页遇到的问题吧 1.插入背景图片并使图片居于div底部充满整个行. <style> background:url(xxx.jpg) no-repeat; backgrou ...

  6. pc端和移动端的轮播图实现(只是结构,内容以后慢慢补充)

    轮播图 PC端 移动端 原生js的写法 图片顺序 8123456781 设置计时器 当过度完成之后判断index是否到达两边界限,是的话设置位移 手指touchstart时,获取位置,暂停计时器 手指 ...

  7. [ Tools ] [ MobaXterm ] [ SSH ] [ Linux ] 中文顯示解決

    預設是無法顯示中文的,需要修改連線的 Terminal Setting

  8. Java中成员变量和局部变量区别

    在类中的位置不同 重点 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围不一样 重点 成员变量:类中 局部变量:方法中 初始化值的不同 重点 成员变量:有默认值 局部变量: ...

  9. Asp.net Core 源码-SessionExtensions

    using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace SportsStore.Infrastructure { publi ...

  10. VMware ESXi定制版(OEM ISO)资源下载(包含5.1\5.5\6.0)

     一.VMware ESXi 5.1.0 update03 1.VMware-ESXi-5.1.0-Update3-2323236-hitachi-0400.iso(日立) 2.VMware-ESXi ...