C - Hat’s Words

HDU - 1247

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<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAX=26;
typedef struct Trie_Node
{bool isWord;struct Trie_Node *next[MAX];Trie_Node(){isWord = false;memset(next,0,sizeof(next));}
}Trie;
char s[50010][50];
void insert(Trie *root,char *word)
{Trie *p=root;int i=0;while(word[i]!='\0'){int j = word[i]-'a';if(p->next[j]==NULL){Trie *temp=new Trie();p->next[j]=temp;}p=p->next[j];i++;}p->isWord=true;
}
bool search(Trie *root,char *word)
{Trie *p=root;for(int i=0;word[i]!='\0';i++){if(p->next[word[i]-'a']==NULL)return false;p=p->next[word[i]-'a'];}return p->isWord;
}
void del(Trie *root)
{for(int i=0;i<MAX;i++){if(root->next[i]!=NULL)del(root->next[i]);}free(root);
}
int main()
{// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int i,j,cnt=0;char str[50];Trie *root=new Trie;while(scanf("%s",s[cnt])!=EOF)insert(root,s[cnt++]);for(i=0;i<cnt;i++){int len=strlen(s[i]);for(j=1;j<len-1;j++){char temp1[50]={'\0'};char temp2[50]={'\0'};strncpy(temp1,s[i],j);strncpy(temp2,s[i]+j,len-j);//    cout<<temp1<<endl;//    cout<<temp2<<endl;if(search(root,temp1)&&search(root,temp2)){printf("%s\n",s[i]);break;}}}del(root);return 0;
}

Hat’s Words HDU - 1247相关推荐

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

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

  2. HDOJ 1247 HDU 1247 Hat’s Words ACM 1247 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址 : http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目描述: ...

  3. HDU 1247(Hat’s Words )

    /* Date: 2012/10/27 题目链接地址:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路:把字典中的每个字符串拆成0->j 和 j ...

  4. HDU(1247):Hat’s Words,BKD_hash

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 大意:给定字典序的一连串字符串,找出其中一个能由其他两个组成的字符串. 用这个题目练习下字符串h ...

  5. HDU 1247 Hat’s Words

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

  6. 【HDU - 1247】Hat’s Words(字典树,预处理,tricks)

    题干: A hat's word is a word in the dictionary that is the concatenation of exactly two other words in ...

  7. Hdu 1247 Hat's Words(Trie树)

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

  8. hdu 1247 字典树

    题意:给你一部分单词,让你找出所有这样的单词:a+b=c,就是一个单词,由另外两个单词串联而成 解题思路:先输入全部单词,构建一棵tire树,然后枚举所有单词,对单词进行查找,查找过程中经过的单词节点 ...

  9. hdu 1247 map

    818786 asia2562219 C Accepted 4464 KB 218 ms C++ 615 B 2012-11-17 16:05:08 写个了字典树 但又发现map 这个以前没用过的 容 ...

  10. 字典树 之 hdu 1247

    /* 放了一天,晚上突然想出其这道题哪里出了问题...悲催的Runtime Error(ACCESS_VIOLATION) 此题思路: 1)建字典树 2)尝试把每个单词拆成两个单词的所有情况,判断这两 ...

最新文章

  1. Windows存储串口数据至txt文件中的教程及代码
  2. 基于FPGA多通道数据采集系统verilog设计
  3. [Nginx]nginx常用的命令
  4. 使用Tensorflow实现简单线性回归
  5. ubuntu18.04新安装时Unable to locate package问题
  6. python多线程实现for循环_Python多线程实现同时执行两个while循环
  7. keep怎么生成运动轨迹_【念叨叨】keep手环拔草记
  8. java static 变量,和方法从属于类
  9. 28.MySQL Variables
  10. FFmpeg学习(8)—— 视频去水印
  11. 如何自学通过PMP?
  12. Loading 加载中 图片素材
  13. 电压比较器工作原理 -----运放入门,重点!!!
  14. HTML中img路径问题
  15. 红外遥控系统原理附51单片机软件解码程序
  16. 程序猿和hr面试时的巅峰对决
  17. OpenCV3 和 Qt5 计算机视觉:1~5
  18. 鞋长度和欧美的标准宽度换算表
  19. Android 装置CTS与GMS认证差异
  20. 20201218英语单词学习(仅供自己记录)

热门文章

  1. Kotlin与Android能做什么?答:Android开发优先语言
  2. Audio / Video Playback
  3. Android JSON 解析库的使用 - Gson 和 fast-json
  4. Utils工具方法集插件详解
  5. MySQL字符集设置及字符转换(latin1转utf8)
  6. Java并发包学习--ReentrantLock
  7. 《纽约时报》:乔布斯最后的日子 与家人相伴
  8. 最简单的《域中隔离用户的FTP站点》详解
  9. 如何解决api接口的并发问题?
  10. [Hdu3555] Bomb(数位DP)