//---------------------------------------------------------------
/*---字典树应用问题。考虑到要查询的次数在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. .net网站数据抓取

    最新项目需要抓取人民币汇率中间价的数据,所以就写了个简单的爬虫抓取数据.抓取的网站为:http://www.safe.gov.cn/wps/portal/sy/tjsj_hlzjj_inquire # ...

  2. PHP文件开头session_start()

    session_start(); 告诉服务器使用session.一般来说,php是不会主动使用session的. 不过可以设置php.ini中的session.auto_start=1来自动对每个请求 ...

  3. table & colgroup

    table & colgroup // <caption>版本信息</caption> table = ` <table class="versions ...

  4. BZOJ1951 [Sdoi2010]古代猪文 【费马小定理 + Lucas定理 + 中国剩余定理 + 逆元递推 + 扩展欧几里得】

    题目 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很久以前,在山的那 ...

  5. NOIP2017赛前考试注意事项总结

     考前: 考试前把读入优化和库以及对拍文件打好做好准备工作,另外注意放松心态,太紧张了肯定考不好··将自己的注意力集中起来  考场策略: 考试的基本策略是对每于道题先想个20分钟,如果想不出个靠谱的方 ...

  6. Waifu2x测试

    真的是好玩..给大家(?)提供一个能用Waifu2x upscale的图片类型集合.. 上面是原图下面是Upscaled..因为是png大家自行下载对比..

  7. 洛谷P1522 牛的旅行

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  8. C# 代码片段

    StringBuilder拼接小技巧 Stopwatch watch = new Stopwatch(); watch.Start(); var sb = new StringBuilder(); ; ...

  9. windows 中使用hbase 异常:java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

    平时一般是在windows环境下进行开发,在windows 环境下操作hbase可能会出现异常(java.io.IOException: Could not locate executable nul ...

  10. tkinter 表格

    import tkinter from tkinter import ttk root = tkinter.Tk() tree = ttk.Treeview(root, show="head ...