Repository HDU2846
极限过的 最原始的方法一层一层建树就好了
#include<bits/stdc++.h>
using namespace std; int trie[][]={};
int sum[]={};
bool flag[]; int root=;
int pos;
void insert1(char *s)
{
int root=;
for(int i=;i<strlen(s);i++)
{ int ch=s[i]-'a';
if( trie[ root ][ch]== )
{
// memset(trie[pos],0,sizeof(trie[pos]));//用多少初始化多少
trie[root][ch]=pos++;
}
root=trie[root][ch];
if( !flag[root] ){flag[root]=true;sum[root]++;}
} }
int find1(char *s)
{
int root=;
for(int i=;i<strlen(s);i++)
{
int ch=s[i]-'a';
if( trie[root][ch]== )return ;
root=trie[root][ch]; } return sum[root]; } int main()
{
pos=;
int n;scanf("%d",&n);
char a[];char s[];
while(n--)
{
memset(flag,false,sizeof(flag));
scanf("%s",a);
int n1=strlen(a);
for(int k=;k<n1;k++)
{
strcpy( s , a+k );
insert1(s);
} }
scanf("%d",&n);
while(n--)
{
scanf("%s",a);
printf("%d\n",find1(a));
} return ;
}
优化了: 时间节省了600ms
1.去掉flag标记数组 改sum为二维数组 更加方便 学会了标记数组不是只有0,1那么死板 多观察就可以不用memset 节省了大量时间!!!
2.直接 a+k带入 不用strcpy到另外一个数组里去了
#include<bits/stdc++.h>
using namespace std; int trie[][]={};
int sum[][]={};
bool flag[]; int root=;
int pos;
void insert1(char *s,int id)
{
int root=;
for(int i=;i<strlen(s);i++)
{ int ch=s[i]-'a';
if( trie[ root ][ch]== )
{
// memset(trie[pos],0,sizeof(trie[pos]));//用多少初始化多少
trie[root][ch]=pos++;
}
root=trie[root][ch];
if( sum[root][]!=id ){sum[root][]=id;sum[root][]++;}
} }
int find1(char *s)
{
int root=;
for(int i=;i<strlen(s);i++)
{
int ch=s[i]-'a';
if( trie[root][ch]== )return ;
root=trie[root][ch]; } return sum[root][]; } int main()
{
pos=;
int n;scanf("%d",&n);
char a[];
while(n--)
{ scanf("%s",a);
int n1=strlen(a);
for(int k=;k<n1;k++)
{ insert1(a+k,n);
} }
scanf("%d",&n);
while(n--)
{
scanf("%s",a);
printf("%d\n",find1(a));
} return ;
}
Repository HDU2846的更多相关文章
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
- hdu2846 Repository
//--------------------------------------------------------------- /*---字典树应用问题.考虑到要查询的次数在10^6,显然直接插入 ...
- 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 ...
随机推荐
- 【BZOJ2749】【HAOI2012】外星人[欧拉函数]
外星人 Time Limit: 3 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output 输出te ...
- JavaScript之从浏览器一键获取教务处个人课程信息【插件】
由于博主的个人网站(:http://www.johnnyzen.cn/),每学期都需要更新呈现课程的静态信息,由于课程量多,而且手动爬取很冗杂,特别想自动化实现.这不,今天终于有点时间了,把之前写no ...
- luogu P3565 [POI2014]HOT-Hotels
传送门 无脑暴力+O2=AC 题目要统计距离两两相等的三个点的组数,这三个点之间显然有一个点,并且这三个点到这个点的距离都相同.所以枚举中间这个点作为根,然后bfs整棵树,对于每一层,把以根的某个儿子 ...
- saltstack系列~第四篇
简介 针对mysql的sls编写0 软件包推送部分 tool_rsync: file.recurse: - source: salt://files/mysql ...
- Spring Boot 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...
- nginx入门一
配置文件: server_name user root; worker_processes 2; error_log logs/error-test.log; #pid logs/nginx.pid; ...
- python httplib和urllib的性能比较
httplib代码: urlParseResult = urlparse(url) host = urlParseResult.hostname path = urlParseResult.path ...
- android手机访问app网页报错:net::ERR_PROXY_CONNECTION_FAILED
手机访问网页报错:net::ERR_PROXY_CONNECTION_FAILED 手机访问app中嵌入的html网页报错: net::ERR_PROXY_CONNECTION_FAILED 原来是手 ...
- Android:Service
Android Service: http://www.apkbus.com/android-15649-1-1.html android service 的各种用法(IPC.AIDL): http: ...
- pytorch实现花朵数据集读取
import os from PIL import Image from torch.utils import data import numpy as np from torchvision imp ...