hdu2846 Repository
//---------------------------------------------------------------
/*---字典树应用问题。考虑到要查询的次数在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的更多相关文章
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
- Repository HDU2846
极限过的 最原始的方法一层一层建树就好了 #include<bits/stdc++.h> using namespace std; ][]={}; ]={}; ]; ; int pos; ...
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)
上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(2)> 这篇文章主要是对 DDD.Sample 框架增加 Transa ...
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- windows 部署 git 服务器报 Please make sure you have the correct access rights and the repository exists.错误
这两天在阿里云上弄windows 服务器,顺便部署了一个git服务.根据网上教程一步步操作下来,最后在 remote远程仓库的时候提示 fatal: 'yourpath/test.git' does ...
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...
- 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 * ...
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)
上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(1)> 阅读目录: 抽离 IRepository 并改造 Reposi ...
随机推荐
- lua中是 ffi 解析 【是如何处理数据包的/pkt是如何传进去的】 fsfsfs
lua中的ffi是如何解析的呢? 拿bcc中对proto的解析说起: metatype是有大学问的: ffi.metatype(ffi.typeof('struct ip_t'), { __index ...
- POJ 3243 Clever Y | BSGS算法完全版
题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...
- BZOJ1823 [JSOI2010]满汉全席 【2-sat】
题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而能够烹饪出经过专家 ...
- 自动设置 rem es模块写法
export default function () { let html = document.documentElement; function onWindowResize() { if (ht ...
- 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法
转载自:http://blog.csdn.net/v_july_v/article/details/8203674/ 从K近邻算法.距离度量谈到KD树.SIFT+BBF算法 前言 前两日,在微博上说: ...
- RHN Classic and Red Hat Subscription Management
What's the difference between RHN Classic and Red Hat Subscription Management? Introduction With the ...
- hdu 4359 dp
/* 题目大意:给n个节点的二叉树第i个节点的权值为2^(i-1), 求所有含左右子树的节点都符合左子树的权和小于右子树权和的种数. */ #include <iostream> #inc ...
- webservice跨域文件,好多年前的东西,远程调用,js服务器端使用,可以远程调用
1.clientaccesspolicy.xml <?xml version="1.0" encoding="utf-8" ?> <acces ...
- .net 过滤 sql防注入类,省地以后每次都要重新弄!
/// <summary> /// 过滤不安全的字符串 /// </summary> /// <param name="Str"&g ...
- 取代VS, sourceISight的IDE神器CLION
https://www.jetbrains.com/clion/download/download-thanks.html 随时升级 http://idea.lanyus.com/ m_pRemoti ...