Hat’s Words

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

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

Author

戴帽子的

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  1251 1075 1671 1298 1800

题意:

给定一些单词,要你从中找到某些单词,而这个单词是由另外两个单词组成的。

其实我们就是利用字符的ascii码来给他对应的索引,就是把每个单词拆成两部分,看看是不是每一部分在字典树中都是一个单词

比如说建树的时候存储apple这个单词,对应如下图

在算法导论中,Trie并不叫字典树,而叫基数树,实际上并不是只与字典树有关,是一个N叉树。字典树的功能是对于很多串进行压缩,压缩的方法是跟据这个字符串的前缀,每个节点表示一个字符,从根节点到叶子节点表示一个字符串。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX = 26;
struct Trie
{Trie *next[MAX];bool isword;
};
Trie *root = new Trie;
char word[50000][30];
void createTrie(char str[])
{int len = strlen(str);Trie *p = root,*q = NULL;for(int i=0;i<len;i++){int id = str[i]-'a';if(p->next[id]==NULL){q = new Trie;for(int j=0;j<MAX;j++)q->next[j] = NULL;q->isword = false;p->next[id] = q;}if(i==len-1)p->next[id]->isword = true;p = p->next[id];}}
bool findTrie(char str[])
{int len = strlen(str);Trie *p = root;for(int i=0;i<len;i++){int id = str[i]-'a';if(p->next[id]==NULL){return false;}p = p->next[id];}if(p->isword)return true;elsereturn false;
}
void del(Trie *root)
{for(int i=0;i<MAX;i++){if(root->next[i]!=NULL){del(root->next[i]);}}delete root;
}
int main()
{int num=0;char str1[30],str2[30];for(int i=0;i<MAX;i++){root->next[i] = NULL;}root->isword = false;while(gets(word[num])){createTrie(word[num]);num++;}for(int i=0;i<num;i++){int len = strlen(word[i]);if(len==1)continue;for(int j=0;j<len;j++)//从每个单词的各部分拆开{int k;if(j==len-1) continue;for(k=0;k<=j;k++){str1[k] = word[i][k];}str1[k]='\0';int k2=0;for(int l=k;l<len;l++){str2[k2++]=word[i][l];}str2[k2]='\0';if(findTrie(str1)&&findTrie(str2)){cout<<word[i]<<endl;break;//居然错在这里了(可能会重复输出)}}}del(root);return 0;
}

HDU1247 字典树 Hat’s Words(Tire Tree)相关推荐

  1. hdu1247 字典树或者hash

    题意:      给你一些串,问你哪些串是由其他两个串连接成的. 思路:        我用了两种方法,一个是hash,hash的时候用map实现的,第二种方法是字典树,字典树我们枚举每个一字符串,查 ...

  2. hdu1247 字典树

    开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...

  3. 字典树,01字典树,可持续化01字典树(总结+例题)

    目录 字典树 01字典树 字典树例题: power oj 2390: 查单词 HDU 1671 Phone List HDU 1004Let the Balloon Rise HDU 1075 Wha ...

  4. 提高篇 第二部分 字符串算法 第3章 Trie字典树

    Trie(字典树)解析及其在编程竞赛中的典型应用举例 - Reqaw - 博客园 『一本通』Trie字典树 - YeLingqi - 博客园 字典树(Trie Tree) - 仰望高端玩家的小清新 - ...

  5. HDU4825 Xor Sum 01字典树(Tire Tree)

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

  6. LeetCode Implement Trie (Prefix Tree)(字典树)

    问题:实现具有insert,search,startsWith方法的字典树 思路:就是要实现Trie数据结构 具体代码参考: https://github.com/wuli2496/OJ/tree/m ...

  7. C++实现trie tree字典树(附完整源码)

    实现trie tree字典树 实现trie tree字典树算法的完整源码(定义,实现,main函数测试) 实现trie tree字典树算法的完整源码(定义,实现,main函数测试) #include ...

  8. 字典树(Trie tree)

    Trie,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字 ...

  9. HDU 1247 Hat’s Words 字典树(Trie树)

    HDU 1247 Hat's Words 字典树(Trie树) 字典树的建立是应该都是一样的 下面是我的做法: 建立完后, 对每一个单词都进行find_string()判断是否符合, 分别对其分成两半 ...

最新文章

  1. spring中是如何解析@Profile注解的
  2. 是什么影响了MySQL性能?
  3. Tkinter的Canvas组件
  4. 【渝粤题库】广东开放大学 Java高级编程技术 形成性考核
  5. Linux进程O(1)调度算法,面试必考哦
  6. springMVC实体用注解管理,多对多 set集合元素排序问题 解决
  7. java (lodop) 打印实例
  8. LSMW--一个中文介绍的摘抄
  9. Thinkpad SL400安装黑苹果10.8.4全纪录
  10. shell_script_查询主机名、ip地址 、DNS地址
  11. 3.2 Tensorflow基础运算
  12. 无线通信设备安装工程概预算编制_浙江正规设备安装工程安装-设计安装_天霖工程...
  13. oracle总结: INTERVAL DAY TO SECOND, 但却获得 NUMBER
  14. 如何使用 JAVA 开发微信登录教程
  15. C语言美化控制台命令
  16. 基于GPU编程的三维重建系统
  17. 概率图模型(马尔可夫模型)
  18. 解决提示“npm audit fix“问题
  19. 【找规律】codeforces 710 F
  20. SpringBoot 中使用HikariPool 报错Possibly consider using a shorter maxLifetime value.

热门文章

  1. JAVA File方法各类文件复制操作
  2. IOS TextField设置大全
  3. Scalaz(9)- typeclass:checking instance abiding the laws
  4. ajax burp 乱码,burp suite中国乱码的解决方案
  5. java actionsupport_struts2中的Action接口和Actionsupport接口各有什么作用
  6. opengl游戏引擎源码_渲染概念:1.引擎二三事
  7. python通讯录管理程序的用户可行性_通讯录管理系统项目可行性分析
  8. mysql底层用什么写的_天天写order by,你知道Mysql底层如何执行吗?
  9. java 方法重载 应用举例,Java 实例 - 重载(overloading)方法中使用 Varargs
  10. java 位运算符赋值_java-运算符(算术、赋值 =、关系、逻辑、三元、位运算符)...