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. Hdu-6249 2017CCPC-Final G.Alice’s Stamps 动态规划

    题面 题意:给你n个集合,每个集合有L到R这些种类的邮票,让你选择其中的K个集合,使得最后选择的邮票种类尽可能多,N,L,R都<=2000 题解:容易乱想到网络流,可是再细想一下就会发现处理不了 ...

  2. ROW_NUMBER() OVER()函数用法;(分组,排序),partition by (转)

    1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排 ...

  3. Android View事件分发与传递

    在Android中,人们主要通过手指与系统交互.Android把所有的touch事件都被封装成MotionEvent来进行处理,其中包括了手指点击的位置,时间等信息.其事件类型主要包括:ACTION_ ...

  4. 小程序-wx:for

    wx:for (列表渲染) index默认数组下标item默认数组当前项的变量名 数组是对象的形式,单纯写{{item}},结果是[object object]的形式,必须加对象名,并且对象名基本设置 ...

  5. 第五周课后作业——适用于人工智能的visual studio 的创新分析

    个人觉得作业布置的(2)(3)(4)(5)的顺序并不合理,我将以(5)(2)(3)(4)的顺序开展我的分析. 创新的定义是做出一些改变或创造出新的东西,既是过程,也是结果.这是一个很泛的概念,所以去问 ...

  6. MeayunDB学习笔记(一) MeayunDB介绍及安装

    系列目录   MeayunDB介绍-高性能分布式内存数据库 MeayunDB学习笔记(一)MeayunDB介绍及安装 MeayunDB学习笔记(二)批量导入数据 MeayunDB学习笔记(三)索引应用 ...

  7. Qt5.2 for Android 配置及部署到手机运行

    使用DNK编程也没有那么难,使用QT为安卓跨平台编程需要安装NDK,SDK通过NDK调用C++程序,偶尔能提高一些效率. SDK下载地址:http://developer.android.com/sd ...

  8. 【技术累积】【点】【java】【4】日志级别

    闲聊 水文也是文,写总比不写好. 日志级别 虽然对其他语言的日志系统也不甚了解,但还是感觉Java的日志有些麻烦,当然也可以说是发展已久,多有变化,多有完善吧. 从日志级别来说,有从高到低的八个级别: ...

  9. 11.05 选择前n个记录

    select ename,salfrom(select (select count(distinct b.sal)from emp bwhere a.sal<=b.sal) as rnk,a.s ...

  10. sass揭秘之@if,@for,@each(转载)

    因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sassmeister这款在线编译工具,方便你阅读学习. 经过上两篇揭秘,大家心里对sass应该有了很好的认知感了,这篇文章基于前面两 ...