Cyclical Periods
Time limit1sMemory limit1024 MB
Given letter strings at sparse positions on a number line, find the letter whose periodic occurrences have the longest period.
- Level
Easy3 of 10
- Topics
- Hash map, Math, Implementation
- Solved
- No attempts yet
Problem

The image illustrates the superposition of four cycles , , , and with different offsets and periods. appears every 3 steps with an offset of 3, every 4 steps with an offset of 2, every 7 steps with an offset of 5, and every 11 steps with an offset of 4. Cycle has the longest period.
Each position on the number line has a string of lowercase letters, and the letters in each string can be in any order. Given these strings and their positions, find the letter that represents the cycle with the longest period. The number line starts at 1 and is at most 65,536 long. Every letter that appears shows up at least twice, and it appears at every position on the number line that matches its offset and period.
Input
The first line contains an integer (), the number of strings on the number line. Each following line describes a position on the number line that has at least one letter. Each such line starts with a positive integer (), the position, followed by a space and the string of lowercase letters at that position. No letter is repeated within a string, and the positions increase strictly from one line to the next.
Output
Print the single letter that represents the cycle with the longest length. If two cycles tie, choose the one that appears at the earliest position. If they still tie, choose the letter that appears first in the string where the cycle is first discovered.