极限过的 最原始的方法一层一层建树就好了

#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的更多相关文章

  1. hdu2846 Repository 字典树(好题)

    把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...

  2. hdu2846 Repository

    //--------------------------------------------------------------- /*---字典树应用问题.考虑到要查询的次数在10^6,显然直接插入 ...

  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. 2017CCPC秦皇岛 L题One-Dimensional Maze&&ZOJ3992【模拟】

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3992 题意: 走迷宫,一个一维字符串迷宫,由'L'.'R'组成,分别 ...

  2. python 历险记(一)— python 的String,集合(List,元组,Dict)

    目录 引言 String 有哪些有用的方法? 如何拼接字符串? 如何分隔字符串? 如何获取字符串长度 如何将 list 拼接成字符串? 如何替换字符串? 如何去除字符串中的空格? 如何子字符串是否包含 ...

  3. DDR3基本知识及测试【转】

    转自:http://blog.csdn.net/myarrow/article/details/7847385 一.DDR3简介 DDR3(double-data-rate three synchro ...

  4. python各种post上传文件

    1.带证书上传文件 filename = '/tmp/test.cert'hash_v = 'assumethisisahash' #这是一种流式上传的方式with open(filename, 'r ...

  5. 单个 LINQ to Entities 查询中的两个结构上不兼容的初始化过程中出现类型“XXXX”

    最近在做一个报表的时候,用EF使用了Contact方法,但是程式运行一直出错.最近终于找到原因了,写下来提醒下自己.好了,进入正题: 现在我举个栗子,目前数据库中有ParentStudent表和Sub ...

  6. saltstack自动化运维系列④之saltstack的命令返回结果mysql数据库写入

    saltstack自动化运维系列④之saltstack的命令返回结果mysql数据库写入salt的返回值写入mysql数据库:可参考:https://docs.saltstack.com/en/lat ...

  7. centos6.5下java和tomcat环境部署

    软件包: tomcat安装包 apache-tomcat-7.0.67.zip jdk安装包 jdk-6u10-linux-x64.bin 下载地址:http://www.oracle.com/tec ...

  8. mysql语句判断是否存在记录,没有则插入新纪录否则不执行

    1 前言 由于项目需要,当某个表如果有记录,就不执行加入语句,否则加入新纪录(测试数据).思路是:判断表的记录是否为空,然后再决定是否插入 2 代码 DROP PROCEDURE IF EXISTS ...

  9. PYTHON-模块定义 搜索路径

    模块是什么: ***** 模块 是一系列功能的集合体 一个py文件就是一个模块 一个函数就是一个功能 例如 A.py 文件名A.py 模块名 A 模块有哪些来源 内置 第三方 自定义 模块有四种通用的 ...

  10. SQL数据库基础知识