地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247

题目:

Hat’s Words

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

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
 

思路:1.用Trie树,枚举每个单词,看他是否由两个单词组成,判断过程其实就是字典树的查找。时间复杂度是O(n*len)

    2.map爆一发,map哈希的复杂度不清楚,不过也是枚举每个单词是否由两个单词组成,枚举过程时间复杂度也是O(n*len),总体复杂度和Trie复杂度应该差不多。

    3.我看到我的运行时间很短,可能暴力枚举加map也能过。枚举str[i],str[j],判断map[str[i]+str[j]]存不存在即可,不知道能过否

代码:

#include <stdio.h>
#include <stdlib.h> using namespace std; #define MAXNUM 26
//定义字典树结构体
typedef struct Trie
{
bool flag;//从根到此是否为一个单词
Trie *next[MAXNUM];
}Trie;
//声明一个根
Trie *root;
char ss[][];
//初始化该根
void init()
{
root = (Trie *)malloc(sizeof(Trie));
root->flag=false;
for(int i=;i<MAXNUM;i++)
root->next[i]=NULL;
}
//对该字典树的插入单词操作
void insert(char *word)
{
Trie *tem = root;
while(*word!='\0')
{
if(tem->next[*word-'a']==NULL)
{
Trie *cur = (Trie *)malloc(sizeof(Trie));
for(int i=;i<MAXNUM;i++)
cur->next[i]=NULL;
cur->flag=false;
tem->next[*word-'a']=cur;
}
tem = tem->next[*word-'a'];
word++;
}
tem->flag=true;
}
bool search2(char *word)
{
Trie *tem = root;
char *p=word;
while(*p)
{
if(tem==NULL||tem->next[*p-'a']==NULL)
return false;
tem=tem->next[*p-'a'];
p++;
}
return tem->flag;
}
//查询一个单词的操作
bool search1(char *word)
{
Trie *tem = root;
for(int i=;word[i]!='\0';i++)
{
tem=tem->next[word[i]-'a'];
if(tem->flag&&search2(&word[i+]))
return ;
}
return ;
} int main(void)
{ int n=;
init();
while(~scanf("%s",ss[n]))
insert(ss[n]),n++;
for(int i=;i<n;i++)
if(search1(ss[i]))
printf("%s\n",ss[i]);
return ;
}

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

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

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

  2. HDU1247 Hat’s Words 【trie树】

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

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

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

  4. HDU1247 Hat’s Words(Trie树)

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

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

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

  6. HDU-1247 Hat’s Words (暴力)【Trie树】

    <题目链接> 题目大意: 给你一些单词,要求输出将该单词完全分成前.后两个单词之后,若这两个单词都在单词库中出现,则输出该单词. 解题分析: 将每个单词的每一位能够拆分的位置全部暴力枚举一 ...

  7. hdu-1247 Hat’s Words---字典树模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出 解题 ...

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

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

  9. Hat’s Words hdu-1247

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

随机推荐

  1. 【position也可以很复杂】当弹出层遇上了鼠标定位(下)

    前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...

  2. jquery实现内容滚动

    HTML代码: <div class="scollNews"> <ul> <li><a href="#">1&l ...

  3. SharePoint 2010 人员选择器搜索范围的限定

    客户AD中用户信息过多,而当前的SharePoint应用中不需要针对所有AD进行筛选,则需要通过STSADM来设置搜索范围: stsadm -o setsiteuseraccountdirectory ...

  4. Android Testing学习01 介绍 测试测什么 测试的类型

    Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...

  5. 浏览器判断是否安装APP

    浏览器判断是否安装APP http://blog.csdn.net/henrywulibin/article/details/52087041 从浏览器中打开我们的应用 http://blog.csd ...

  6. spring理解

    Struts与Hibernate可以做什么事? Struts,Mvc中控制层解决方案,可以进行请求数据自动封装.类型转换.文件上传.效验… Hibernate,持久层的解决方案:可以做到,把对象保存到 ...

  7. 2016春招Android开发实习生(网易传媒)笔试

    一.单选题 1.下列不属于网络层协议的为 TCP IP IPX ICMP 2.关于activity的状态恢复,错误的是 onSaveInstanceState中,activity会自动收集恢复view ...

  8. 获取设备IMEI ,手机名称,系统SDK版本号,系统版本号

    1.获取设备IMEI TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Str ...

  9. Android 常见对话框

    1.对话框通知(Dialog Notification) 当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用对话框来完成. 下面代码将打开一个如图所示的对话框: public void ...

  10. 关于alpha透明度

    使用场景 一个半透明的框上面   放了一些label     改变半透明view的alpha    结果 label的  alpha 也跟着改变了. 其实不用那么麻烦   给你们UI  要一个色值也可 ...