#C11531. Folder Size Aggregation
Folder Size Aggregation
Folder Size Aggregation
You are given a list of folder paths along with their sizes in bytes. Each folder path is a string that represents the full directory path (using '/' as the separator) and its corresponding size in bytes. Your task is to compute the aggregated size for every folder path including the sizes of all of its subfolders.
More formally, for a given folder path \(F\) with an initial size \(s_F\), for every folder path \(P\) that is an ancestor of \(F\) (i.e. \(F = P/\ldots\)), add \(s_F\) to the total of \(P\). The output should list every folder encountered (both the ones provided in the input and those computed as parents) along with their aggregated sizes.
For example, consider the following input:
4 /a 10 /a/b 20 /a/b/c 5 /d 3
The aggregated sizes will be computed as follows:
- Folder \(/a/b/c\): 5
- Folder \(/a/b\): 20 + 5 = 25
- Folder \(/a\): 10 + 20 + 5 = 35
- Folder \(/d\): 3
Print the results in lexicographical order (sorted by folder path). The first line of input contains the number of folders \(N\), and the following \(N\) lines each contain a folder path and its size, separated by a space.
Note: If a parent folder is not explicitly provided in the input, you should still compute and output its aggregated size if it gets updated through its subfolders.
The formula to update a parent's folder size can be represented in \(\LaTeX\) as:
\[ S_{parent} = S_{parent} + S_{child} \]inputFormat
The input is read from standard input (stdin). It starts with an integer \(N\) representing the number of folder entries. Each of the next \(N\) lines contains a folder path (a string) and an integer size (in bytes), separated by a space.
N folder_path_1 size_1 folder_path_2 size_2 ... folder_path_N size_N
outputFormat
Output to standard output (stdout) the aggregated sizes for each folder path. Each line should contain a folder path and its aggregated size separated by a space. The output should be sorted in lexicographical order based on the folder paths.
folder_path_1 aggregated_size_1 folder_path_2 aggregated_size_2 ...## sample
4
/a 10
/a/b 20
/a/b/c 5
/d 3
/a 35
/a/b 25
/a/b/c 5
/d 3
</p>