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 ...
随机推荐
- Dojo与jQuery综合比较分析
最近一直都在参与项目,无法抽空写些或者看些东西,周末抽了点时间看了下关于Dojo和Jquery的东西,在CSDN上看到一篇两个框架进行对比的文章,感觉写的不错,就拿了过来,没有别的意思,一来想保留下来 ...
- hadoop HA 配置 + zookeeper 服务注册
环境测试 6台机器 centos6.7 x64 master ( namenode/cluster ) 10.10.100.101 namenode1 10.10.100.105 namenode2 ...
- C#实现office文档转换为PDF格式
1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 OfficeSave ...
- uboot 如何向内核传递参数
a.uboot 向内核传递的参数有两种类型 1.一个是bootargs 2.一个是环境参数, 而环境参数的设置靠的是 Y:\junda\JdLinuxApp\A1801_uboot\source\u- ...
- Shiro缓存(十三)
使用缓存,可以解决每次访问请求都查数据库的问题.第一次授权后存入缓存. 缓存流程 shiro中提供了对认证信息和授权信息的缓存.shiro默认是关闭认证信息缓存的,对于授权信息的缓存shiro默认开启 ...
- SRS+flv.js打造兼容性较高的直播、点播平台
**************************************************************************************************** ...
- ROS学习笔记(一) # ROS参数服务器
参考 roscpp/Overview/Parameter Server 0. 概述 ROS参数服务器能够保存 string, int, float, double, bool, list, dicti ...
- genstr.py
#!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...
- composer安装laravel框架时未生成Vendor解决办法
三个方法并没有关联,可以单独尝试也可以一起设置. 方法一. 去php.ini中查看下面三个扩展项是否开启 extension=php_fileinfo.dll extension=php_mbstri ...
- WDS 三种模式
(1)懒人模式(Lazy mode) 此模式下不需要填写对端的BSSID,本端AP的WDS连接作为被动连接,只需要对端填写了本端AP的BSSID即可,效果和桥接模式一样. (2)桥接模式(Bridge ...