一个很经典的字典树题目

先建树 再拆单词进行判断是否都在树内

因为爆内存错了很久

如果一个四十万的数组  用mamset的话会直接爆几十万的内存

所以要:用多少 初始化多少才对!( 修改了两条初始化语句 见代码)  不过这题只有一组数据  所以不初始化关系不大

#include<bits/stdc++.h>
using namespace std; int trie[][]={};
int sum[];
char ans[][];//这里数组开小了导致一直wa
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],,sizeof(trie[pos]));//用多少初始化多少
trie[root][ch]=pos++; }
root=trie[root][ch];
}
sum[root]=;
} bool find1(char *s)
{
int root=;
for(int i=;i<strlen(s);i++)
{
int ch=s[i]-'a';
if( trie[root][ch]== )return false;
root=trie[root][ch]; }
return sum[root]; } int main()
{ int c=;
pos=;
memset(sum,,sizeof(sum));
memset(trie[],,sizeof(trie[]));//用多少初始化多少
while(gets(ans[c++]))
{
insert1( ans[c-] );
} for(int i=;i<=c-;i++)
{ for(int k=;k<strlen( ans[i] ) ;k++)
{
char a[],b[];
strncpy(a,ans[i],k );a[k]='\0';
strncpy(b,ans[i]+k, strlen(ans[i]) );
b[strlen(ans[i]) -k]='\0';
if(find1(b)&&find1(a))
{
puts(ans[i]);break; }
} } return ;
}

Hat’s Words HDU1247的更多相关文章

  1. Hat’s Words hdu-1247

    就是查找这个单词能不能有两个单词组成,简单的字典树题目 ////////////////////////////////////////////////////////////// #include& ...

  2. Tire树(字典树)

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

  3. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

  4. HDU1247 Hat’s Words(Trie的动态内存版本)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. HDU1247 Hat’s Words 【trie树】

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. 【Trie】【HDU1247】【Hat’s Wordsfd2】

    题目大意: hat's word 的定义是字典中 恰好由另外两个单词连接起来的单词 给你一本字典,问有多少个hat's word,(字典按字典序给出) 单词数50000.. 初步思路: 单词分为前缀单 ...

  7. HDu-1247 Hat’s Words,字典树裸模板!

    Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...

  8. HDU1247 Hat’s Words(Trie树)

    常规做法是枚举每个字符串每个位置,时间复杂度O(n*len*len),(建字典树O(n*len)). 然而我看这题第一眼想的是时间复杂度O(n*len)的算法..就是建正反两棵字典树,每个字符串跑分别 ...

  9. HDU1247 - Hat’s Words(Trie树)

    题目大意 给定一些单词,要求你把所有的帽子单词找出来,如果某个单词恰好由另外两个单词连接而成,那么它就是帽子单词 题解 先把所有单词插入到Trie树,然后判断每个单词是不是帽子单词,做法就是:对于第i ...

随机推荐

  1. mod_wsgi 的两种模式

    mod_wsgi 的两种模式 http://ssmax.net/archives/977.html http://www.cnblogs.com/yuxc/p/3555005.html mod_wsg ...

  2. semantic segmentation 和instance segmentation

    作者:周博磊链接:https://www.zhihu.com/question/51704852/answer/127120264来源:知乎著作权归作者所有,转载请联系作者获得授权. 图1. 这张图清 ...

  3. 【CXF】: No binding operation info while invoking unknown method with params unknown.

    CXF发布webservice之后访问报错: org.apache.cxf.interceptor.Fault: No binding operation info while invoking un ...

  4. Spring+SpringMVC+Mybatis整合(二)

    目录结构:

  5. 深层揭密extern "C"

    一. extern "C" 包含双重含义,从字面上即可得到:首先,被它修饰的目标是“extern”的:其次,被它修饰的目标是“C”的.让我们来详细解读这两重含义. (1) 被ext ...

  6. stderr 和stdout

    今天又查了一下fprintf,其中对第一个参数stderr特别感兴趣. int fprintf(FILE *stream,char *format,[argument]): 在此之前先区分一下:pri ...

  7. CC254x/CC2540/CC2541库函数速查(转)

    hci.h 转自:http://blog.csdn.net/xiaoleiacmer/article/details/44036607#t1 //分配内存,应用程序不应该调用这个函数. void *H ...

  8. saltstack自动化运维系列⑤之saltstack的配置管理详解

    saltstack自动化运维系列⑤之saltstack的配置管理详解 配置管理初始化: a.服务端配置vim /etc/salt/master file_roots: base: - /srv/sal ...

  9. vmware下centos克隆功能对网络的设置

    centos完成克隆功能后需要对网络进行设置 # cd /etc/udev/rules.d/ vim 70-persistent-net.rules 删除eth0的配置,将eth1该为eth0 编辑网 ...

  10. centos7 Firewalld操作集合

    =============================================== 2019/4/15_第1次修改                       ccb_warlock == ...