地址: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[50006][100];
//初始化该根
void init()
{root = (Trie *)malloc(sizeof(Trie));root->flag=false;for(int i=0;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=0;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=0;word[i]!='\0';i++){tem=tem->next[word[i]-'a'];if(tem->flag&&search2(&word[i+1]))return 1;}return 0;
}int main(void)
{int n=1;init();while(~scanf("%s",ss[n]))insert(ss[n]),n++;for(int i=1;i<n;i++)if(search1(ss[i]))printf("%s\n",ss[i]);return 0;
}

转载于:https://www.cnblogs.com/weeping/p/5932318.html

hdu1247 Hat’s Words相关推荐

  1. hdu1247 Hat’s Words

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

  2. hdu1247(Hat’s Words)

    我以为像a.aa这样的输入应该是没有输出的,结果还是要输出aa. 建树的时候就是常规建树,不过查找的时候要做一些变形:对于一个单词,从第一位检查有没有单词是它的前缀,如果有的话,再去检查它的后半部分是 ...

  3. 题目梳理(一)(2019.07.06~2019.07.20)

    一.字典树类型 A   HihoCoder1014 Trie树(已做) 题目链接    https://hihocoder.com/problemset/problem/1014 B    HDU12 ...

  4. HDU1247 字典树 Hat’s Words(Tire Tree)

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

  5. Hat’s Words(HDU-1247)

    Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly tw ...

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

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

  7. Hat’s Words(字典树)

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

  8. Red Hat Linux 安装教程

    一.下载链接 链接:https://pan.baidu.com/s/1JShQmOrgGG5_uaqPUuaHLg  提取码:ture 二.安装步骤 1.打开虚拟机,单击"创建新的虚拟机&q ...

  9. 在Red Hat Linux5下构建LAMP网站服务平台之MySQL、PHP的安装与配置

    在Red Hat Linux5下构建LAMP网站服务平台之MySQL.PHP的安装与配置 2010-09-09 16:40:49 标签:PHP Linux mysql RedHat [推送到技术圈] ...

最新文章

  1. python以运行效率高著称吗_如何提高python的运行效率
  2. 华为海思HISILICON
  3. Flink从入门到精通100篇(二十)-跨境电商 Shopee 的实时数仓之路
  4. [一]RabbitMQ-客户端源码之ConnectionFactory
  5. C#模拟POST表单提交 --- WebClient
  6. 斗地主AI算法——第四章の权值定义
  7. 从零开始学前端:表格制作 --- 今天你学习了吗?(CSS:Day04)
  8. 计算机网络的简单概述以及在浏览器中输入一个网址后如何执行
  9. cad黑色背景变成白色_学会了、让你前程似锦的CAD完整版教程
  10. 团队作业7——Alpha冲刺之事后诸葛亮
  11. 计算机网络实验 Go Back N (带有ACK)滑动窗口协议 C++
  12. Cocos Creator入门实战:桌球小游戏
  13. AI会让AI工程师失业吗?
  14. win10给扩展屏设置单独的壁纸(win10自带这个功能)
  15. 连接网络显示dns服务器不可用,上不了网,提示DNS服务器不可用如何解决
  16. D35 Spark源代码(待补充)
  17. id 和 class的区别
  18. vagrant搭建nginx、php环境,两个项目间使用curl请求问题
  19. 如何两周快速通过 阿里云大数据分析师acp认证(及免费获取ACA认证资格)
  20. 【112期分享】5款知识竞答PPT模板免费下载

热门文章

  1. 动态可订制属性的 PropertyGrid(转载)
  2. (9)C#之ADO.NET 两个ListBox联动案例
  3. form表单提交不刷新页面的方法
  4. eureka 和zookeeper 区别 优势【转】
  5. 正睿 2018 提高组十连测 Day4 T3 碳
  6. SQL With As 用法Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
  7. 2017.04.15【NOIP2017提高组】模拟赛B组 T2:渡河
  8. SQL server 200 安装问题!
  9. Google Chrome 调试JS利器
  10. 【Groovy】自定义 Xml 生成器 BuilderSupport ( nodeCompleted 方法获取节点闭合信息 )