#C11129. Group Anagrams
Group Anagrams
Group Anagrams
You are given a list of words and your task is to group the anagrams together. Two words are anagrams if one word can be formed by rearranging the letters of the other. In this problem, you need to:
- Read an integer n representing the number of words.
- Read n words (each on a separate line).
- Group the words that are anagrams of each other.
- For each group, sort the words in lexicographical order.
- Then, sort the groups in ascending order based on the lexicographically smallest word in each group.
- Output the number of groups, followed by each group on a new line, with words separated by a single space.
Note: When comparing words, use LaTeX formatting for any formulas. For example, consider the sorted form of a word \(w\) as \(sorted(w)\). All input is provided via standard input and the output must be printed on standard output.
inputFormat
The first line of the input contains an integer n \( (1 \le n \le 10^5)\) — the number of words. The following n lines each contain a word consisting of lowercase English letters. Some words may be empty.
outputFormat
First, print an integer \(k\), the number of groups of anagrams. Then, print \(k\) lines, each line containing a group of anagrams. In each group, the words should be sorted in lexicographical order, and the groups should be ordered in ascending order based on the first word in each group. Words in a group are separated by a single space.
## sample6
eat
tea
tan
ate
nat
bat
3
ate eat tea
bat
nat tan
</p>