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 ...
随机推荐
- tensorflow实现mnist
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 在变量的构建时,通过trunc ...
- EF crud操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- hadoop - 基础操作
查看文件: [root@hadoop hadoop]# hadoop fs -lsr /drwxr-xr-x - root supergroup 0 2016-12-27 10:49 /homedrw ...
- Python之print(args)与sys.stdout.write(string)使用总结
一.sys.stdout.write(string) import sys; # sys.stdout.write(): # 1.默认不换行 # 2.参数必须是字符串 # demo 01 x = &q ...
- 【SVN】svn使用方法
下载安装TortoiseSVN 下载地方 安装成功后 TortoiseSVN清除凭证 右击空白处-TortoiseSVN-Settings打开Settings窗口后做如下操作: svn在idea中的使 ...
- mysql 架构 ~异地容灾
一 简介 我们来探讨下多机房下的mysql架构二 目的: 首先要清楚你的目的 1 实现异地机房的容灾备份 2 实现异地机房的双活 三 叙说 1 实现异地机房的容灾备份 ...
- python - class类 (一)
三大编程范式 1.面向过程 2.函数式 3.面向对象 注意 编程范式没有高低之分,只有适用不适用. 面向对象编程: 编程是程序源用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一 ...
- 使用block的时候,导致的内存泄漏
明确,只要在block里边用到我们自己的东西,成员变量,self之类的,我们都需要将其拿出来,把它做成弱指针以便之后进行释放. 在ZPShareViewController这个控制器中,由如下代码: ...
- Cpp读文件、CString转String、String转CString
场景 C++读取文件 技术点 读取文件 fstream提供了三个类,用来实现c++对文件的操作.(文件的创建.读.写). ifstream -- 从已有的文件读入 ofstream -- 向文件写内容 ...
- python3之Django多数据库
1.定义数据库 在django项目中, 一个工程中存在多个APP应用很常见:有时候希望不同的APP连接不同的数据库,这个时候需要建立多个数据库连接.在Django的setting中使用DATABASE ...