Permutation
SRM 160 · 2003-08-23 · by dgoodman
Problem Statement
We want to find the permutation of the first n letters of
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzthat has the longest cycle. Create a class Permutation that contains the method best that takes the number of characters n as input and returns the lexicographically first permutation that has the maximum possible cycle length.
"Lexicographically first" means the
Constraints
- n is between 1 and 52 inclusive
6 Returns: "ACBEFD"
This permutation has cycle length 6. So does BDEFCA, but ACBEFD is lexicographically first. The cycle is: ABCDEF -> ACBEFD -> ABCFDE -> ACBDEF -> ABCEFD -> ACBFDE -> ABCDEF
7 Returns: "BCAEFGD"
This is the lexicographically first permutation with cycle length 12
29 Returns: "BCDEAGHIJKLFNOPQRSTMVWXYZabcU"
1 Returns: "A"
8 Returns: "BCAEFGHD"
Submissions are judged against all 53 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Permutation with a public method string best(int n) · 53 test cases · 2 s / 256 MB per case