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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int trie[][]={};
  5. int sum[]={};
  6. bool flag[];
  7.  
  8. int root=;
  9. int pos;
  10. void insert1(char *s)
  11. {
  12. int root=;
  13. for(int i=;i<strlen(s);i++)
  14. { int ch=s[i]-'a';
  15. if( trie[ root ][ch]== )
  16. {
  17. // memset(trie[pos],0,sizeof(trie[pos]));//用多少初始化多少
  18. trie[root][ch]=pos++;
  19. }
  20. root=trie[root][ch];
  21. if( !flag[root] ){flag[root]=true;sum[root]++;}
  22. }
  23.  
  24. }
  25. int find1(char *s)
  26. {
  27. int root=;
  28. for(int i=;i<strlen(s);i++)
  29. {
  30. int ch=s[i]-'a';
  31. if( trie[root][ch]== )return ;
  32. root=trie[root][ch];
  33.  
  34. }
  35.  
  36. return sum[root];
  37.  
  38. }
  39.  
  40. int main()
  41. {
  42. pos=;
  43. int n;scanf("%d",&n);
  44. char a[];char s[];
  45. while(n--)
  46. {
  47. memset(flag,false,sizeof(flag));
  48. scanf("%s",a);
  49. int n1=strlen(a);
  50. for(int k=;k<n1;k++)
  51. {
  52. strcpy( s , a+k );
  53. insert1(s);
  54. }
  55.  
  56. }
  57. scanf("%d",&n);
  58. while(n--)
  59. {
  60. scanf("%s",a);
  61. printf("%d\n",find1(a));
  62. }
  63.  
  64. return ;
  65. }

优化了:  时间节省了600ms

1.去掉flag标记数组 改sum为二维数组  更加方便  学会了标记数组不是只有0,1那么死板 多观察就可以不用memset 节省了大量时间!!!

2.直接 a+k带入  不用strcpy到另外一个数组里去了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int trie[][]={};
  5. int sum[][]={};
  6. bool flag[];
  7.  
  8. int root=;
  9. int pos;
  10. void insert1(char *s,int id)
  11. {
  12. int root=;
  13. for(int i=;i<strlen(s);i++)
  14. { int ch=s[i]-'a';
  15. if( trie[ root ][ch]== )
  16. {
  17. // memset(trie[pos],0,sizeof(trie[pos]));//用多少初始化多少
  18. trie[root][ch]=pos++;
  19. }
  20. root=trie[root][ch];
  21. if( sum[root][]!=id ){sum[root][]=id;sum[root][]++;}
  22. }
  23.  
  24. }
  25. int find1(char *s)
  26. {
  27. int root=;
  28. for(int i=;i<strlen(s);i++)
  29. {
  30. int ch=s[i]-'a';
  31. if( trie[root][ch]== )return ;
  32. root=trie[root][ch];
  33.  
  34. }
  35.  
  36. return sum[root][];
  37.  
  38. }
  39.  
  40. int main()
  41. {
  42. pos=;
  43. int n;scanf("%d",&n);
  44. char a[];
  45. while(n--)
  46. {
  47.  
  48. scanf("%s",a);
  49. int n1=strlen(a);
  50. for(int k=;k<n1;k++)
  51. {
  52.  
  53. insert1(a+k,n);
  54. }
  55.  
  56. }
  57. scanf("%d",&n);
  58. while(n--)
  59. {
  60. scanf("%s",a);
  61. printf("%d\n",find1(a));
  62. }
  63.  
  64. return ;
  65. }

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. Dojo与jQuery综合比较分析

    最近一直都在参与项目,无法抽空写些或者看些东西,周末抽了点时间看了下关于Dojo和Jquery的东西,在CSDN上看到一篇两个框架进行对比的文章,感觉写的不错,就拿了过来,没有别的意思,一来想保留下来 ...

  2. hadoop HA 配置 + zookeeper 服务注册

    环境测试 6台机器 centos6.7 x64 master ( namenode/cluster ) 10.10.100.101 namenode1 10.10.100.105 namenode2 ...

  3. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

  4. uboot 如何向内核传递参数

    a.uboot 向内核传递的参数有两种类型 1.一个是bootargs 2.一个是环境参数, 而环境参数的设置靠的是 Y:\junda\JdLinuxApp\A1801_uboot\source\u- ...

  5. Shiro缓存(十三)

    使用缓存,可以解决每次访问请求都查数据库的问题.第一次授权后存入缓存. 缓存流程 shiro中提供了对认证信息和授权信息的缓存.shiro默认是关闭认证信息缓存的,对于授权信息的缓存shiro默认开启 ...

  6. SRS+flv.js打造兼容性较高的直播、点播平台

    **************************************************************************************************** ...

  7. ROS学习笔记(一) # ROS参数服务器

    参考 roscpp/Overview/Parameter Server 0. 概述 ROS参数服务器能够保存 string, int, float, double, bool, list, dicti ...

  8. genstr.py

    #!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...

  9. composer安装laravel框架时未生成Vendor解决办法

    三个方法并没有关联,可以单独尝试也可以一起设置. 方法一. 去php.ini中查看下面三个扩展项是否开启 extension=php_fileinfo.dll extension=php_mbstri ...

  10. WDS 三种模式

    (1)懒人模式(Lazy mode) 此模式下不需要填写对端的BSSID,本端AP的WDS连接作为被动连接,只需要对端填写了本端AP的BSSID即可,效果和桥接模式一样. (2)桥接模式(Bridge ...