#K71987. Simplify Unix Path
Simplify Unix Path
Simplify Unix Path
Given a Unix-style file path, simplify it into its canonical form. The path may include segments such as .
(which represents the current directory) and ..
(which indicates moving one directory up). For absolute paths (those starting with /
), the simplified result must also start with /
. For relative paths, do not prepend a slash.
For example, given the path /home//foo/
, your program should output /home/foo
. Similarly, /a/./b/../../c/
should simplify to /c
and a/./b/c/
should simplify to a/b/c
.
The task is to remove redundant slashes, current directory markers, and correctly apply parent directory moves to render the simplest valid path. Any formulaic or symbolic expressions (e.g., directory moves) can be referenced with LaTeX if needed, such as using \ldots
for ellipsis, but this problem is primarily string manipulation.
inputFormat
A single line is provided from the standard input (stdin), representing a Unix-style file path. The input string may contain multiple segments separated by '/'.
outputFormat
Output the simplified Unix-style file path to the standard output (stdout).## sample
/home//foo/
/home/foo