hdu 1247 Hat’s Words
2016-04-05 19:13
344 查看
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
Sample Output
思路:将每个单词进行拆分为两部分,看一下是否能在字典树里找到他。
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 <cstdio> #include <cstring> #include <algorithm> #define MAXNUM 26 using namespace std; char words[50005][100]; typedef struct Trie { bool flag; Trie *next[MAXNUM]; }Trie; Trie *root; 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 search(char *word) { Trie *tem = root; for (int i = 0;word[i] != '\0';i++) { if (tem == NULL || tem->next[word[i] - 'a'] == NULL) return false; tem = tem->next[word[i] - 'a']; } return tem->flag; } int main() { init(); int t = 0; char w[100]; while (scanf("%s", words[t]) != EOF) { insert(words[t]); t++; } for (int i = 0;i<t;i++) { memset(w, '\0', sizeof(w)); for (int j = 0;words[i][j] != '\0';j++) { *(w + j) = *(words[i] + j); if (search(w) && search((words[i] + j + 1))) { printf("%s\n", words[i]); break; } } } return 0; }
相关文章推荐
- J2EE 开发中遇到的问题
- 第五周项目3(1)
- git 学习1
- linux环境下的jdk安装配置
- 第六周项目二-带武器的游戏角色
- 构造器权限与类权限的问题
- 二分法查找
- 求最大公约数
- 第五周项目1-三角形雏形(3)
- 怎样配置vi编辑器
- 第六周项目3-IP地址类
- Form token KO80SIJW4F84034NG5HM1ZBUGOVNY64D does not match the session token null.
- Integer.valueof(null)报错
- Leetcode 96. Unique Binary Search Trees
- bzoj 3265 志愿者招募加强版 (单纯形)
- 初识commonjs
- 数据分类K—means 算法的python代码实现
- HDU 4059 The Boss on Mars 容斥原理+逆元
- ytu 2617: B C++时间类的运算符重载
- this关键字