//---------------------------------------------------------------
/*---字典树应用问题。考虑到要查询的次数在10^6,显然直接插入后dfs来查找必然超时间。好在每一个单词长度
---不超过20,这样可以枚举每个单词子串,然后插入即可。例如abc子串为a,b,c,ab,bc,abc。但是要注意的是同一个
---串可能有相同的子串,避免重复插入,可以采用一个节点信息num表示当前插入的是第num个串的子串,可以避免重复插入
*/
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string.h>
typedef long long LL;
using namespace std;
const int maxsize = 26; struct Trie{
Trie(){ memset(next, 0, sizeof(next)); v = 0; num = -1; }
int v;
int num; //标记当前插入的是第num个单词的子串
Trie*next[maxsize];
}; Trie*T;
void insert(int num,char*str){
Trie*p = T;
while (*str != '\0'){
int id = *str++ - 'a';
if (!p->next[id]) p->next[id] = new Trie;
p = p->next[id];
}
if (num != p->num){
p->num=num;
p->v++;
}
} int search(char*str){
Trie*p=T;
while (*str != '\0'){
int id = *str++ - 'a';
if (!p->next[id])return 0;
p = p->next[id];
}
return p->v;
} char str[maxsize];
char temp[maxsize]; int main(){
int n, m;
T = new Trie;
scanf("%d", &n);
while (n--){
scanf("%s", str);
int len = strlen(str);
for (int l = 1; l <= len;l++)
for (int i = 0; i+l<=len;i++){
memcpy(temp, str+i, l);
temp[l] = '\0';
insert(n, temp);
}
}
scanf("%d", &m);
while (m--){
scanf("%s", str);
printf("%d\n",search(str));
}
return 0;
}

  

hdu2846 Repository的更多相关文章

  1. hdu2846 Repository 字典树(好题)

    把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...

  2. Repository HDU2846

    极限过的 最原始的方法一层一层建树就好了 #include<bits/stdc++.h> using namespace std; ][]={}; ]={}; ]; ; int pos; ...

  3. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)

    上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(2)> 这篇文章主要是对 DDD.Sample 框架增加 Transa ...

  4. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  5. windows 部署 git 服务器报 Please make sure you have the correct access rights and the repository exists.错误

    这两天在阿里云上弄windows 服务器,顺便部署了一个git服务.根据网上教程一步步操作下来,最后在 remote远程仓库的时候提示 fatal: 'yourpath/test.git' does ...

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

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

  7. Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。

    写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...

  8. Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx

    问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...

  9. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)

    上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(1)> 阅读目录: 抽离 IRepository 并改造 Reposi ...

随机推荐

  1. icon fonts generator & svg

    icon fonts generator https://icomoon.io/app/#/select https://icomoon.io/ http://fontastic.me/ http:/ ...

  2. 2151: 种树 - BZOJ

    Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n.并且每个位置都有一个美观度 ...

  3. 关于flink的时间处理不正确的现象复现&原因分析

    跟朋友聊天,说输出的时间不对,之前测试没关注到这个,然后就在processing模式下看了下,发现时间确实不正确 然后就debug,看问题在哪,最终分析出了原因,记录如下:    最下面给出了复现方案 ...

  4. BZOJ2879 [Noi2012]美食节 【费用流】

    题目 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他很快就尝遍了美食节所有的美食.然而,尝鲜的欲望是难以满足的.尽管所有的菜品都很可 ...

  5. 算法复习———dijkstra求次短路(poj3255)

    题目: Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  6. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  7. freescale 16位单片机的地址映射

    以MC9S12XS128MAL为例,其实DG128之类的类似.如图一,128代表的是单片机中的FLASH大小为128K Byte,同理64代表的是单片机中的FLASH大小为64 K Byte,256代 ...

  8. Linux : 使用 lsof 恢复文件

    用 lsof 命令在某种程度上可以恢复删除的文件, 前提是这个文件被正在运行的进程占用. 比如: 日志文件, 配置文件. lsof 恢复文件 查找需要恢复的文件和占用文件的进程 PID lsof |g ...

  9. Activity介绍

    1.Activity使用方法(跳转): (1)写Activity类继承Activity package com.example.test2; import android.app.Activity; ...

  10. NOIP2011提高组

    D1T1.铺地毯 for循环 #include<iostream> #include<cstdio> #include<algorithm> using names ...