#C4826. Group Anagrams

    ID: 48407 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a list of strings. Your task is to group the words that are anagrams of each other. An anagram is a rearrangement of the letters of a word to form another word. For example, the words ( \texttt{eat}, \texttt{tea}, \texttt{ate} ) are all anagrams of each other.

Input is given via standard input. The first line contains an integer ( n ) representing the number of words. The second line contains ( n ) words separated by spaces. Note that a word may be an empty string.

Your task is to group these words into groups of anagrams. For each group, sort the words in lexicographical order. Then, sort the groups in lexicographical order by the first word of each group. Finally, output the groups: each group is printed on a new line with its words separated by a single space.

inputFormat

The input begins with an integer ( n ) (( 1 \leq n \leq 10^5 )), representing the number of words. The next line contains ( n ) words separated by spaces. Words consist of lowercase English letters (a–z) or may be empty.

outputFormat

Output the groups of anagrams, one group per line. In each group, the words are sorted in lexicographical order, and the groups themselves are sorted in lexicographical order by their first word. Each word in a group should be separated by a single space.## sample

6
eat tea tan ate nat bat
ate eat tea

bat nat tan

</p>