#DSU0000035. Dịch vòng bảng chữ cái (Phase Shift)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dịch vòng bảng chữ cái (Phase Shift)
Phase Shift
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Take the lowercase English letters and arrange them secretly around one circle, with every letter appearing exactly once. To encode a string , replace every letter with the next letter clockwise on the circle; the last letter is followed by the first. This produces an encoded string .
You know but not the circular ordering. A valid ordering must contain all letters in one single cycle, not several disjoint cycles. Every occurrence of the same letter is encoded using the same mapping.
Recover the lexicographically smallest possible original string among all strings that could produce under some valid circular ordering.
Input
The first line contains . The second line contains a length- string consisting only of letters a–z.
Output
Print the lexicographically smallest possible .
Subtasks
- Subtask 1 (20 points): ; ; contains only lowercase letters
a–z. - Subtask 2 (30 points): ; ; contains only lowercase letters
a–z. - Subtask 3 (50 points): ; ; contains only lowercase letters
a–z.
Examples
Input
2
ba
Output
ac
Explanation
Two encoded letters must have distinct predecessors, and no short cycle is allowed. The smallest valid original string is ac.