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

#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. listcontrolc插入列时,出现断言错误

    原因:窗口还未创建,就对listcontrol进行了操作 解决方案:在初始化函数中 添加CDialogEx::OnInitDialog();

  2. c#中如何在一个panel中放入窗体

    Form2 f2 = new Form2(); //实例化窗体FORM2 f2.TopLevel = false; //设置为非顶级窗体 f2.FormBorderStyle = FormBorder ...

  3. pandas数据表

    安装  pip3 install pandas s=pd.Series([1,3,6,90,44,1])   #创建序列[用列表创建].数据源的维度必须是一维 #data  指定数据源 print(s ...

  4. 第14月第17天 automaticallyAdjustsScrollViewInsets contentInsetAdjustmentBehavior

    1. automaticallyAdjustsScrollViewInsets self.edgesForExtendedLayout = UIRectEdgeNone; if ([self resp ...

  5. 变量,id()

    >>> a = 1 >>> print id(a) 2870961640 >>> b = a >>> print id(b) 2 ...

  6. SpringMVC——SpringMVC简介

    Spring web mvc 和Struts2 都属于表现层的框架,它是Spring 框架的一部分,我们可以从Spring 的整体结构中看得出来:

  7. JavaScript对象复制(二)

    <script> function copy(a) { ret = {}; for (sth in a) { temp = a[sth]; if (temp instanceof Arra ...

  8. python模块分析之logging日志(四)

    前言 python的logging模块是用来设置日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块 ...

  9. classfication中使用图像金字塔和sliding windows提高准确率

    之前对imagenet的预训模型进行finetune,找出了很多样本选择时的注意事项,当时在测试如下这张照片时,效果不好,我认为是物体过小造成的,因此尝试使用图像金字塔的方法: 当时结果如下: 一开始 ...

  10. maven项目有红叉,感叹号如何解决?

    红色感叹号,pom.xml文件有红叉 修改了Maven私服服务器的IP地址.可在Maven安装路径下的conf/setting.xml中修改ip地址,具体参照“开发工具”/maven.工程中class ...