Hat’s Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6912 Accepted Submission(s): 2543
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.
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.
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
思路:年前学得Trie树,当时没过,可能当时没完全理解吧,今天碰到了,就凭着理解直接敲的,调了两次终于过了,把每个单词结尾标记为1,查询的时候看某个单词的子串是否存在,这题WA点不少,必须搞清判断条件。
1 #include<cstdio> 2 #include<string> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 class Node{ 8 public: 9 int cnt; 10 Node *child[26]; 11 Node(){ 12 cnt = 0; 13 for(int i = 0;i < 26;i ++) child[i] = NULL; 14 } 15 }; 16 Node *root = new Node(); 17 char str[50005][111]; 18 void Update(char *str){ 19 Node *tmp = root; 20 while(*str != '