//op标记这次查询是查询后缀还是查询整个 bool searchTrie(Trie *r, char s[], int op) { int i = 0, j; while(s[i]) { j = s[i++] - 'a'; if(r->chi[j] == NULL) returnfalse; r = r->chi[j]; if(!op && r->isEnd && s[i])//前缀是一个单词 { if(searchTrie(root, s + i, 1)) returntrue; //查询后缀是否是一个单词 } } return op ? r->isEnd : op; }
int main() { int m = 0; root = newTrie(); while(~scanf("%s", ss[m])) insertTrie(root, ss[m++]);
for(int i = 0; i < m; ++i) if(searchTrie(root, ss[i], 0)) puts(ss[i]);
return0; }
Hat’s Words
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