传送门: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. Java规则引擎drools:drt动态生成规则并附上具体项目逻辑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...

  2. 从零开始的全栈工程师——js篇2.6

    函数 Var 是js的关键字,用于声明变量,声明在内存模块完成,定义(=)是在执行模块完成. Var可以在内存模块提前(js代码执行前)完成所以有变量提升这个功能. 因为a没有带var,所以不存在变量 ...

  3. maven 搭建springMvc+mybatis

    1.在resource文件夹下创建Configure.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  4. java网络编程—TCP(1)

    演示tcp的传输的客户端和服务端的互访. 需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息. 客户端: 1,建立socket服务.指定要连接主机和端口. 2,获取socket流中的输出流. ...

  5. [转]linux C 打印当前时间

    #include <stdio.h> #include <time.h> int main(void) { time_t t; time(&t); printf(&qu ...

  6. Spring Boot : Swagger 2

    每次修改完代码需要找原本的API时楼主的内心是痛苦的,因为一般情况下都找不到,需要重新写一份.如果使用Swagger的话,只要加几个注解就可以实时生成最新的在线API文档,而且不仅仅是文档,同时支持A ...

  7. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  8. Api容器在应用架构演化中的用途

    单层架构 在最开始编程的时候相信大家都写过下面这种架构,界面代码,业务代码,数据库连接全部在工程面完成.当然这种架构在处理很小的程序的时候依然有生命力 两层架构 后来我们发现数据访问的代码大量重复,应 ...

  9. PHP:implode(),emplode() 字符串数组,数组字符串转换函数

    1.implode()-Join array elements with a string(把数组元素组合为一个字符串.) string implode([string $separator,] ar ...

  10. IOS CALayer基本使用 (图层)

    ● 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层(CALayer) ● 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView 的l ...