CDPlayer
SRM 343 · 2007-03-22 · by connect4
Problem Statement
You recently purchased a CD player from Bob's Bargain Barn. As you don't like listening to songs from your CDs in the same order every day, you were very interested in the "Random" button on the CD player. According to the instruction manual, if the CD player has n songs, the "Random" feature works as follows:
- Randomly select a permutation of the n songs.
- Play the songs in order from that permutation.
- Go to step 1.
You are not sure if you trust Bob's Bargain Barn, so you want to figure out if the CD player is broken or not. However, as luck would have it, your sister had started listening to the CD, so you don't know when the next permutation begins. This means that the list "BBAC" could be an acceptable list, if the first B was the last song in the first permutation, and the second B started the second permutation.
You will be given a
Constraints
- songlist will contain between 1 and 50 elements, inclusive.
- Each element of songlist will contain between 1 and 50 characters, inclusive.
- Each character in songlist will be one of the first n uppercase letters ('A'-'Z').
- n will be between 1 and 26, inclusive.
{"BBAC"}
3
Returns: 1
The example from the problem statement. The first song cannot be the start of a permutation, since "BBA" is not a permutation of "ABC". However, if the permutation starts at song 1, then "??B" and "BAC" are both valid.
{"BACAB",
"BCA"}
3
Returns: 2
Index 0 is illegal because the second set of songs "ABB" is illegal. Similarly, index 1 can't be a legal start ("BBC" is illegal). Index 2 works though, since "?BA", "CAB", "BCA" could be generated by the algorithm.
{"AAACBACBACBACBACBACBACB"}
3
Returns: -1
Even though all of the songs starting at index 2 work, the "?AA" that would have preceded it could not have been generated; thus, the CD player is broken.
{"ABCDEABDECBAECDEDACB"}
5
Returns: 0
{"ABCABCABCABCABCABCABC",
"ABCABCABCABCABCABCABC"}
22
Returns: -1
Submissions are judged against all 126 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CDPlayer with a public method int isRandom(vector<string> songlist, int n) · 126 test cases · 2 s / 256 MB per case