Problem 133
Simplify Path
Medium · 20 pts · String, Stack
Problem
You are given an absolute Unix-style file path, which always starts with '/'. Simplify it to its canonical form. A single period '.' means the current directory and can be dropped. A double period '..' means the parent directory, so it removes the previous directory name; at the root it does nothing. Several slashes in a row count as one slash. The result starts with a single slash, has exactly one slash between directory names, and has no trailing slash, except for the root itself, which is just '/'. Split the path on '/' and use a stack of directory names.
Input
A single absolute path without spaces.
Output
Print the simplified path.
Constraints
1 <= length <= 3000; the path consists of letters, digits, '.', '/' and '_' and starts with '/'
Example input
/home/
Example output
/home
Explanation
In "/home/", the only directory name is home, and the trailing slash is dropped, so the result is "/home". As another example, "/a/./b/../../c/" simplifies to "/c".