Cyclical Periods

시간 제한1초메모리 제한1024 MB

요약
주어진 위치마다 등장하는 알파벳들의 등장 간격을 이용해 각 문자의 주기와 시작 위치를 구하고, 가장 긴 주기를 가진 문자를 출력한다.
난이도

보통10점 중 6점

유형
수학, 해시맵, 구현
정답자
아직 제출이 없습니다

문제

The image illustrates the superposition of four cycles aa, bb, cc, and dd of differing offsets and periods. It can be quickly seen that aa appears every 3 steps with an offset of 3, bb every 4 steps with an offset of 2, cc every 7 steps with an offset of 5, and dd every 11 steps with an offset of 4. Cycle dd can be seen to have the longest period. The first sample input describes the case in the image.

Given a sequence of strings of lowercase letters (arbitrary ordering of letters in each string) and their positions on the number line, your task is to determine which letter represents the cycle with the longest period. The number line will start at 1 and will not be longer than 65,53665{,}536. Any letter that appears will appear at least twice, and it will appear on the entire number line at locations consistent with its offset and period.

입력

The first line contains an integer NN (2≤N≤65,5362 \leq N \leq 65{,}536), representing the number of strings on the number line. Each subsequent line describes a position on the number line that has at least one letter associated with it. Each description line begins with a positive integer PP (1≤P≤65,5361 \leq P \leq 65{,}536) representing the position on the number line followed by a space, and then the string of lowercase letters associated with this position. No letters will be repeated within a string, and the positions are strictly increasing from one line to the next.

출력

Print the single letter that represents the longest cycle length. In case of a tie, use the cycle that appears at the earliest position. If there is still a tie, use the letter that appears first in the string when the cycle is first discovered.

예제2

  1. 예제 1

    입력
    18
    2 b
    3 a
    4 d
    5 c
    6 ab
    9 a
    10 b
    12 ac
    14 b
    15 ad
    18 ab
    19 c
    21 a
    22 b
    24 a
    26 bcd
    27 a
    30 ab
    
    예상 출력
    d
    
  2. 예제 2

    입력
    4
    1 pz
    2 d
    4 zp
    5 d
    
    예상 출력
    z