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.
You are to find all the hat’s words in a dictionary.
InputStandard 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.
OutputYour 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
题目意思:给N个字符串,求出其中有那些字符串可以由二个其他的字符串组成;
解题思路:采用字典树的方法,储存每一个字符串,再将每一个字符串拆分,看能否在字典树中查找到;
1 #include <string.h>
2 #include <iostream>
3 #include<cstdlib>
4 #define MAX 26
5 using namespace std;
6
7 typedef struct TrieNode
8 {
9 bool isStr;
10 struct TrieNode *next[MAX];
11 }Trie;
12
13 void insert(Trie *root,const char *s)
14 {
15 if(root==NULL||*s=='