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 ...
随机推荐
- aes加密码
js地址 https://github.com/yves8888/crypto-js 下面src<!DOCTYPE html> <html lang="en"&g ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- [bzoj3218] a+b problem [最小割+数据结构优化建图]
题面 传送门 思路 最小割 我们首先忽略掉那个奇♂怪的限制,就有一个比较显然的最小割模型: 建立源点$S$和汇点$T$ 对于每个元素$i$建立一个点$i$,连边$<S,i,w[i]>$和$ ...
- HDU3605:Marriage Match IV
Marriage Match IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- template.js 模板引擎
例子: html代码: <div id="box"></div> css代码: table{ border-collapse: collapse; text ...
- 行为型设计模式之职责链模式(Chain of Responsibility)
结构 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 适用性 有多个的对象可以处理一个请求,哪个 ...
- Winform 模拟Session
背景 在Web中Session的功能很好用,于是想Winform中实现该功能,典型应用场景则是登陆成功后,当一段时间不操作,则该会话过期,提示重新登陆. 资源下载 测试代码 示例说明:登陆进去10s不 ...
- jQuery操作下拉列表以及单选多选框
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 通过OpenGL ES在iOS平台实践增强现实(一)
http://ios.9tech.cn/news/2013/1108/38495.html 1.本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据设 ...
- css3实现图片划过一束光闪过效果
该效果在京东里的图片有. .img { display:block; position: relative; width:800px; height:450px; margin:0 auto; } . ...