#K77467. Relative Path Computation

    ID: 34871 Type: Default 1000ms 256MiB

Relative Path Computation

Relative Path Computation

Given two absolute Unix-style file paths, compute the relative path from the source to the target. In other words, by reducing the longest common prefix between the source and target paths, construct the path needed to navigate from the source directory to the target file or directory.

For example, if the source is /foo/bar and the target is /foo/bar/baz, then the relative path is baz. If the source is /foo/bar and the target is /foo/baz, then the relative path is ../baz.

The formulation of the relative path can be summarized in LaTeX as follows:

( relative_path = \underbrace{\texttt{..}/\texttt{..}/\cdots/\texttt{..}}{\text{for each extra component in source}} + \texttt{/} + \underbrace{\texttt{target1/target2/...}}{\text{remaining target components}} )

Write a program that reads two lines from standard input (the first line is the source path and the second line is the target path) and prints the relative path to standard output.

inputFormat

The input consists of two lines read from standard input:

  1. The first line is the source absolute path.
  2. The second line is the target absolute path.

outputFormat

Output the computed relative path from the source to the target as a single line to standard output.## sample

/foo/bar
/foo/bar/baz
baz

</p>