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

#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. js 分页问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. asp.net mvc 中[Authorize]在IE9以上版本关于FormsAuthentication.SetAuthCookie无效的问题 解决方案

    简单的解决方法是,在网站根目录,新增一个浏览器定义文件(browser definition file) 叫“App_Browsers”文件夹,然后里面放一个“IE10.browser”文件即可,网站 ...

  3. Android关于RAM、ROM、SD卡以及各种内存的区别

    RAM:运行时内存.相当于PC机的内存存储,用于存储应用运行时的各种对象和变量常量等,主要作用在于提高运行速度.是唯一一种断电后数据会清除的存储器. 机身内存:相当于PC机硬盘.主要包括三块区域:RO ...

  4. Python 爬虫学习

    #coding:utf-8 #author:Blood_Zero ''' 1.获取网页信息 2.解决编码问题,通过charset库(默认不安装这个库文件) ''' import urllib impo ...

  5. V4L2文档翻译(一)【转】

    转自:https://blog.csdn.net/airk000/article/details/23218903 相关资料 https://www.kernel.org/doc/Documentat ...

  6. HTML学习笔记04-样式

    HTML<style>属性 style属性的作用: 提供了一种改变所有HTML元素样式的通用方法 background-colco属性为元素定义了背景颜色: <!DOCTYPE HT ...

  7. 【转】通过xml处理sql语句时对小于号与大于号的处理转换

    当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...

  8. Python单元测试unittest - 单元测试框架

    一.unittest简介 unitest单元测试框架最初是有JUnit的启发,它支持测试自动化,共享测试的设置和关闭代码,将测试聚合到集合中,以及测试与报告框架的独立性. 二.unittest相关概念 ...

  9. centos中进程管理工具

    进程管理:  二进制的格式为ELF,是CPU指令集中的指令  程序=指令+数据,  进程是程序的副本,可以有多个  内核是一个资源调度监视器  Linux是抢占式多任务  内存被事先划分成多个相同大小 ...

  10. Node.js ECONNREFUSED错误

    1 现象 node服务器 遇见此错误,如下:events.js:71throw arguments[1]; // Unhandled 'error' event^Error: connect ECON ...