EllysDirectoryListing
SRM 534 · 2011-11-22 · by espr1t
Problem Statement
- If "." and ".." are the last two elements of files (in any order), you are done.
- Find the first element of files that is either "." or "..". Swap it with the last element of files.
- If "." and ".." are now the last two elements of files (in any order), you are done.
- Find the first element of files that is either "." or "..". Swap it with the element of files that is one position before the last one.
Notes
- Swapping elements at positions i and j means that the element at position i is moved to the position j and vice versa.
Constraints
- files will contain between 2 and 50 elements, inclusive.
- All elements of files will be distinct.
- Exactly one element of files will be ".".
- Exactly one element of files will be "..".
- Each element of files will contain between 1 and 20 characters, inclusive.
- Each element of files will contain only uppercase and lowercase letters and dots ('A'-'Z', 'a'-'z', '.').
{"ContestApplet.jnlp", ".", "Image.jpg", "..", "Book.pdf", "Movie.avi"}
Returns: {"ContestApplet.jnlp", "Movie.avi", "Image.jpg", "Book.pdf", "..", "." }
The directories are not the last two elements, so we search for the first one (in this case "."), and swap it with the last element (in this case "Movie.avi"). As "." and ".." are still not the last two elements, we search for the second one (in this case "..") and swap it with "Book.pdf".
{"Image.jpg", "..", "."}
Returns: {"Image.jpg", "..", "." }
In this case the directories are already the last two elements, so we do nothing.
{"..", ".", "Image.jpg"}
Returns: {"Image.jpg", ".", ".." }
After swapping ".." with "Image.jpg", the directories are already the last two elements, so we are done.
{"No", "..", "Zaphod", ".", "Just", "very", "very...", "Improbable"}
Returns: {"No", "Improbable", "Zaphod", "very...", "Just", "very", ".", ".." }
{"www.topcoder.com", "Ever.tried", ".", "Ever.failed", "..", "No", "Matter.", "Try", "Again.", "Fail", "Again..", "Fail.Better"}
Returns: {"www.topcoder.com", "Ever.tried", "Fail.Better", "Ever.failed", "Again..", "No", "Matter.", "Try", "Again.", "Fail", "..", "." }
{"This", ".", "is", "tricky", "test", ".."}
Returns: {"This", "test", "is", "tricky", "..", "." }
The first swap moves ".." to position 1, so we need to move it again to index 4 (indexed from zero).
Submissions are judged against all 80 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class EllysDirectoryListing with a public method vector<string> getFiles(vector<string> files) · 80 test cases · 2 s / 256 MB per case