Problem 740
Complete String
Medium · 20 pts · Trie, String
Problem
A word from the list is complete if every one of its prefixes (including the word itself) also appears in the list. For example, ninja is complete if n, ni, nin, ninj and ninja are all in the list. Find the longest complete word; if several have that length, print the lexicographically smallest. If no word is complete, print NONE. Insert all words into a trie and check each word by walking down it, requiring an end-of-word mark at every step.
Input
The first line contains N. The second line contains N words of lowercase letters.
Output
Print the longest complete word (lexicographically smallest on ties), or NONE.
Constraints
1 <= N <= 100000; 1 <= |word|; the total length of all words is at most 100000
Example input
6 n ni nin ninj ninja ninga
Example output
ninja
Explanation
In n ni nin ninj ninja ninga, the word ninja is complete because n, ni, nin, ninj and ninja are all present. ninga is not, because ning is missing. So the answer is ninja.