← 返回 meta 的题目列表Resolve a Unix cd Path Against a Current Working Directory
类型:online_judge
Implement Unix-style path resolution. Given a current working directory cwd (guaranteed to be absolute) and a path argument path to a cd command, return the normalized absolute path after executing cd path.
Rules:
If path starts with /, it is absolute and cwd is ignored.
Otherwise, path is relative to cwd.
. means the current directory; .. means the parent directory. Applying .. at root stays at root.
Repeated / characters are equivalent to one /.
Directory names consist of letters, digits, _, -, and .. Only complete components . and .. have special meaning.
The result must have no trailing slash unless it is /.
Input Format
Two lines: cwd, then path.
Example
Input
/home/user/projects
../../docs/./api
Output
/home/docs/api
Constraints
The combined path length is at most 2 * 10^5.
Example
Input
/home/user/projects
../../docs/./api
Output
/home/docs/api