Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11894    Accepted Submission(s): 4239

Problem Description
A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.
 
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
a
ahat
hat
hatword
hziee
word
 
Sample Output
ahat
hatword
 题意:‘帽子’单词:由其他两个单词拼接而成。输出所有的帽子单词。字典树。
静态建树:
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int N=;
struct node{
bool val;
node* next[N];
};
node* root;
node memory[MAXN];
int ant; node* create()
{
node* p=&memory[ant++];
for(int i=;i<N;i++)
{
p->next[i]=NULL;
p->val=false;
}
return p;
} void insert(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=create();
p=p->next[k];
}
p->val=true;//若能走到最末端,则返回true;
} bool search(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return false;
p=p->next[k];
}
return p->val;//若只是某个已插入单词的前缀,则返回false;
} int main()
{
int cnt=;
root=create();
char word[MAXN][];
while(scanf("%s",word[cnt])!=EOF)
{
//if(word[cnt][0]=='0') break;
insert(word[cnt]);
cnt++;
}
for(int i=;i<cnt;i++)
{
for(int j=;word[i][j];j++)
{
char fr[]={'\0'};
char re[]={'\0'};
strncpy(fr,word[i],j);
strncpy(re,word[i]+j,strlen(word[i])-j);
if(search(fr)&&search(re))
{
printf("%s\n",word[i]);
break;
}
}
} return ;
}

动态建树:

#include"cstdio"
#include"cstring"
#include"cstdlib"
using namespace std;
const int MAXN=;
const int N=;
struct Node{
bool x;
Node* next[N];
Node()
{
x=false;
for(int i=;i<N;i++) next[i]=NULL;
}
};
char word[MAXN][];
int cnt;
Node *root;
void Insert(char s[])
{
Node *p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=new Node();
p=p->next[k];
}
p->x=true;
}
bool Search(char s[])
{
Node *p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return false;
p=p->next[k];
}
return p->x;
}
void Del(Node *p)
{
for(int i=;i<N;i++)
{
if(p->next[i]!=NULL)
{
Del(p->next[i]);
}
}
delete p;
}
int main()
{
root=new Node();
while(scanf("%s",word[cnt])!=EOF)
{
//if(word[cnt][0]=='0') break;
Insert(word[cnt]);
cnt++;
}
for(int i=;i<cnt;i++)
{
for(int j=;word[i][j+];j++)
{
char fr[]={'\0'};
char re[]={'\0'};
strncpy(fr,word[i],j);
strncpy(re,word[i]+j,strlen(word[i])-j);
if(Search(fr)&&Search(re))
{
printf("%s\n",word[i]);
break;
}
}
}
Del(root);
return ;
}
 

HDU1247(经典字典树)的更多相关文章

  1. hdu1247(字典树+枚举)

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

  2. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  3. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  4. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  5. hdu 1247:Hat’s Words(字典树,经典题)

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

  6. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

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

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

随机推荐

  1. ORDER BY today_used ASC' % (MAX_USED_TIMES)

    python D:\pymine\clean\spider_map\get_bd_uid_rest_b.py python D:\pymine\clean\spider_map\get_bd_uid_ ...

  2. appium报'Command 'D\:\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s “adb device” shell pm clear appPackage' exited with code 1'

    解决方法:是因为手机开发者模式没有允许USB调试(安全模式),打开即可

  3. oracle decode的用法

    需求:分别统计emp表中1980,1981,1982,1987年入职的同事的数量. 这里用decode很容易就解决了: select sum(t.num_1980) as "1980&quo ...

  4. Spring注解式与配置文件式

    http://tom-seed.iteye.com/blog/1584632 Spring注解方式bean容器管理 1.通过在配置文件中配置spring组件注入 <context:compone ...

  5. 美团offer

    首先说明我是OP岗,RD的可能没有参考意义.本人985渣本一枚,非计算机.网络相关专业.不得不说美团的面试官给我的感觉很好,首先他们都比较极客,都是各个方向的大牛.虽然根据面试流程必须问我一些与我方向 ...

  6. 第一篇 css导入方式 及选择器

    一 推荐资料 推荐书籍 css Zen Garden 中文(css禅意花园) 二.css样式 1.css样式表特征 继承性  大多数css的样式规则可以被继承 层叠性 1)可以定义 多个样式 2)不冲 ...

  7. Logstash添加Protobuf解析插件

    logstash收集日志时,日志部分是由google的protobuf工具打印的,直接利用json解析会造成部分无法解析的问题 搜索后发现有个logstash的protobuf插件 在logstash ...

  8. Spring Cloud之Hystrix服务保护框架

    服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...

  9. Spring Cloud之服务治理(注册发现)

    服务治理SpringCloud Eureka 什么是服务治理 在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用.负载均衡.容 ...

  10. JavaWeb -- 服务器传递给Servlet的对象 -- ServletConfig, ServletContext,Request, Response

    1.  ServletConfig  有一些东西不合适在程序中写死,应该写在web.xml中,比如 文字怎么显示, 访问数据库名 和 密码, servlet要读取的配置文件 等等.. l在Servle ...