传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846

Repository

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

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


解题心得:

  1. 都知道字典树就是用来查前缀的,但是这个题它询问的字符串在多少个前面给出的字符串中出现过,不仅仅是在前缀中,刚开始使用KMP怼了一波,TLE,然后还是字典树,思想很暴力也很朴实,用给出的串的每一个后缀来建树

    • 例如其中一个串是abbbc,那么在建树的时候要将abbbc,bbbc,bbc,bc,c分别插入树中,但是为了防止一个串被重复计算,例如abab,要将字符串插入的时候做好标记,如果是一个串那么就不用重复加了。
  2. HDU上提交的时候交C++,G++会MLE,黑人问号。

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e5+100;
struct trie
{
int sum,num;
trie *child[26];
trie()
{
sum = num = 0;
for(int i=0;i<26;i++)
child[i] = NULL;
}
};
trie *newnode,*current;
trie *root = new trie;
char s[30]; void insert_tree(int p,int num)//第num个串从第p个字符开始的后缀建树
{
int len = strlen(s);
current = root;
for(int i=p;i<len;i++)
{
int k = s[i] - 'a';
if(current->child[k] == NULL)
{
newnode = new trie;
newnode->sum = 1;
newnode->num = num;
current->child[k] = newnode;
current = newnode;
}
else
{
current = current->child[k];
if(current->num != num)//如果标记相同说明是同一个串分开的,就不用再加了
{
current->num = num;
(current->sum)++;
}
}
}
} int query()
{
current = root;
int len = strlen(s);
for(int i=0;i<len;i++)
{
int k = s[i] - 'a';
if(current->child[k] == NULL)
return 0;
current = current->child[k];
}
return current->sum;
} int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
int len = strlen(s);
for(int j=0;j<len;j++)
insert_tree(j,i);
}
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int ans = query();
printf("%d\n",ans);
}
return 0;
}

HDU:2846-Repository的更多相关文章

  1. hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others)     ...

  2. C#进阶系列——DDD领域驱动设计初探(二):仓储Repository(上)

    前言:上篇介绍了DDD设计Demo里面的聚合划分以及实体和聚合根的设计,这章继续来说说DDD里面最具争议的话题之一的仓储Repository,为什么Repository会有这么大的争议,博主认为主要原 ...

  3. C#进阶系列——DDD领域驱动设计初探(三):仓储Repository(下)

    前言:上篇介绍了下仓储的代码架构示例以及简单分析了仓储了使用优势.本章还是继续来完善下仓储的设计.上章说了,仓储的最主要作用的分离领域层和具体的技术架构,使得领域层更加专注领域逻辑.那么涉及到具体的实 ...

  4. DDD领域驱动设计初探(二):仓储Repository(上)

    前言:上篇介绍了DDD设计Demo里面的聚合划分以及实体和聚合根的设计,这章继续来说说DDD里面最具争议的话题之一的仓储Repository,为什么Repository会有这么大的争议,博主认为主要原 ...

  5. DDD领域驱动设计初探(三):仓储Repository(下)

    前言:上篇介绍了下仓储的代码架构示例以及简单分析了仓储了使用优势.本章还是继续来完善下仓储的设计.上章说了,仓储的最主要作用的分离领域层和具体的技术架构,使得领域层更加专注领域逻辑.那么涉及到具体的实 ...

  6. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  7. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. hdu(2846)Repository

    Problem Description When you go shopping, you can search in repository for avalible merchandises by ...

  9. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

随机推荐

  1. 冷笑话,idea 按删除键就是undo?

    第一反应是keymap被改了,一看 那么,看起来就是alt出问题了 解决方法 在公司换一个键盘或者狂按alt

  2. OracleJDK与OpenJDK的区别和联系

    OpenJDK原是SunMicrosystems公司为Java平台构建的Java开发环境(JDK)的开源版本,完全自由,开放源码.OracleJDK里面包含的JVM是HotSpotVM,HotSpot ...

  3. Hibernate笔记2

    一.持久化类 1.持久化标识OID     数据库中叫做主键,对应实体的ID属性即为OID;Hibernate通过OID区分两个对象是否为同一对象;OID的生成一般交由程序自动处理; 2.持久化类   ...

  4. String.format()的用法

    string.format()用法 2011-06-21 14:58:57|  分类: 工作笔记 |  标签:string  format用法   |字号大中小 订阅 1.格式化货币(跟系统的环境有关 ...

  5. 《Python高效开发实战》实战演练——基本视图3

    在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...

  6. AppScale3.01

    平台: Ubuntu 类型: 虚拟机镜像 软件包: appscale appscale-tools app appscale appserver basic software cloud comput ...

  7. C#环形缓冲区(队列)完全实现

    公司项目中经常设计到串口通信,TCP通信,而且大多都是实时的大数据的传输,然后大家都知道协议通讯肯定涉及到什么,封包.拆包.粘包.校验--什么鬼的概念一大堆,说简单点儿就是要一个高效率可复用的缓存区. ...

  8. 【洛谷1110】[ZJOI2007] 报表统计(两棵平衡树)

    点此看题面 大致题意: 有一个序列,三种操作.\(INSERT\)操作是在原数列第\(i+1\)个元素之前插入一个新元素\(k\),\(MIN\)\(GAP\)操作是查询相邻两个元素的之间差值的最小值 ...

  9. Flutter /bin/sh: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory

    自己写项目中遇到的一个问题, 可以出来是路径找不到,应该是FLUTTER_ROOT这个全局变量没有取到值的原因 1.检查xcode_backend.sh 是否真的存在 2.网上说的:Target -& ...

  10. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...