#C4719. Group Shifted Strings
Group Shifted Strings
Group Shifted Strings
You are given a list of words. Two words belong to the same shifting sequence if the differences between every two adjacent characters are the same modulo 26. More formally, for a word with characters \(w_0, w_1, ..., w_{k-1}\), its shifting key is defined by the sequence \(\left[(w_1-w_0) \bmod 26, (w_2-w_1) \bmod 26, \dots, (w_{k-1}-w_{k-2}) \bmod 26\right]\). For words of length 1, consider them as a special group.
Your task is to group all words that share the same shifting key. In the output, each group should have its words sorted in lexicographical order, and the groups themselves should be sorted by the lexicographically smallest word (i.e. the group leader). Each group is printed on a single line where the words are separated by a single space.
Note: The shifting difference is calculated in a cyclic manner over the alphabet. That is, the difference between 'a' and 'z' is \( (\text{ord}('a') - \text{ord}('z') + 26) \bmod 26 \).
inputFormat
The first line contains an integer \(n\) (where \(n \ge 1\)), representing the number of words. Each of the following \(n\) lines contains one word composed of lowercase English letters.
outputFormat
Output the groups, one group per line. For each group, print the words sorted lexicographically and separated by a single space. The groups should be ordered in ascending order according to the lexicographically smallest word in each group.
## sample8
abc
bcd
acef
xyz
az
ba
a
z
a z
abc bcd xyz
acef
az ba
</p>