#K49582. Simplify File Paths

    ID: 28674 Type: Default 1000ms 256MiB

Simplify File Paths

Simplify File Paths

You are given one or more file paths. Your task is to simplify each file path by removing redundant parts.

The rules for simplification are as follows:

  • A single dot "." indicates the current directory and should be removed.
  • A double dot ".." moves up one directory, meaning that the previous directory should be removed if present.
  • Consecutive slashes ("//") should be treated as a single slash ("/").
  • The simplified path must begin with a slash "/" if it is an absolute path.
  • Remove any trailing slash unless the simplified path is the root "/".

Note: All formulas or conditions mentioned above in LaTeX are:

  • "." represents the current directory.
  • ".." represents moving up one level, e.g., if we have a path segment \( A/.. \), it should be simplified to remove both A and \(..\).

Implement a solution that reads input from stdin and writes the results to stdout.

inputFormat

The first line contains an integer \(n\) representing the number of file paths. The following \(n\) lines each contain a file path string.

Constraints:

  • \(1 \leq n \leq 100\)
  • Each file path is a non-empty string of length at most 300 characters.

outputFormat

Output \(n\) lines. Each line should contain the simplified file path corresponding to the input.

## sample
1
/home/foo/
/home/foo

</p>