博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU Problem 1247 Hat's Words 【字典树】
阅读量:7178 次
发布时间:2019-06-29

本文共 2523 字,大约阅读时间需要 8 分钟。

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13908    Accepted Submission(s): 4987

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:            

题意:给你n个单词,处理到EOF。问这些单词中有几个单词是由两个拼接成的

 

思路:

用字典树,枚举每个单词,找到这个单词每个节点上的单词结尾标志,记录,然后检查剩下的单词出没出现在字典树上。

#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#define LOACL#define space " "using namespace std;typedef long long LL;typedef __int64 Int;typedef pair
paii;const int INF = 0x3f3f3f3f;const double ESP = 1e-5;const double PI = acos(-1.0);const int MOD = 1e9 + 7;const int MAXN = 100 + 10;char str[50000 + 10][30];// 字典树struct trie { int cnt; //记录该节点下的单词数目 trie* next[30]; //以每个单词创建子树 bool endd; //该节点是否为单词的最后一位} *root;//创建根节点void build_trie() { root = new trie; for(int i = 0;i < 27; i++) root->next[i] = NULL;}void insert_trie(char s[]) { int len = strlen(s); trie *p = root, *q; for (int i = 0; i < len; i++) { int idx = s[i] - 'a'; //如果该字母没有出现 if (p->next[idx] == NULL) { q = new trie; q->cnt = 0; q->endd = false; for (int j = 0; j < 30; j++) { q->next[j] = NULL; } p->next[idx] = q; } p = p->next[idx]; //记录此时的单词数目 p->cnt++; } //标记结尾 p->endd = true;}bool query_trie(char s[]) { trie *p = root; int len = strlen(s); int num[50000]; int px = 0; for (int i = 0; i < len; i++) { int idx = s[i] - 'a'; if(p->next[idx]->endd) num[px++] = i; p = p->next[idx]; } for (int i = 0; i < px; i++) { p = root; int j = num[i] + 1; for (; j < len; j++) { int idx = s[j] - 'a'; if(p->next[idx] == NULL) break; p = p->next[idx]; if(j == len - 1 && p->endd) return true; } } return false;}int main() { build_trie(); int t = 0; while (scanf("%s", str[t]) != EOF) {insert_trie(str[t]); t++;} for (int i = 0; i < t; i++) { if (query_trie(str[i])) printf("%s\n", str[i]); } return 0;}

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770750.html

你可能感兴趣的文章
微软工作这二年
查看>>
二叉搜索树
查看>>
复旦版最佳医院排行 沪21家医院入选全国百佳
查看>>
PHP file_get_contents设置超时处理方法
查看>>
ylbtech-LanguageSamples-XMLdoc
查看>>
27 Best Free Eclipse Plug-ins for Java Developer to be Productive
查看>>
Android应用icon和闪屏splash的尺寸
查看>>
C++强制类型转换操作符 dynamic_cast
查看>>
Image Lazy Load:那些延时加载图片的开源插件(jQuery)
查看>>
VC++ 中ListCtrl经验总结
查看>>
我在tmux中最不可少的配置: 用鼠标切换窗口/调节分屏大小
查看>>
tamper绕WAF详解
查看>>
Static Classes and Static Class Members
查看>>
Linux 索引节点(inode)详解
查看>>
[saiku] 简化/汉化/设置默认页
查看>>
使用nginx搭建https服务器(转)
查看>>
Hibernate注解
查看>>
[转]World Wind Java开发之四——搭建本地WMS服务器
查看>>
3D数学基础:四元数与欧拉角之间的转换
查看>>
算法导论:二叉搜索树
查看>>