#C1364. Most Recent Files Extraction
Most Recent Files Extraction
Most Recent Files Extraction
You are given a list of filenames that follow a specific format: base_version.txt
. Here, base
is a string consisting of alphabets and possibly other characters, and version
is a positive integer. Formally, each filename can be represented as:
[ \text{filename} = \text{base}_\text{version}.txt ]
Your task is to select, for each distinct base, the file with the highest version number. The output should list these filenames sorted in lexicographical order of their base names.
For example, given the filenames:
data_5.txt
report_2.txt
data_3.txt
image_10.txt
data_7.txt
report_8.txt
the correct output is:
data_7.txt image_10.txt report_8.txt
since data_7.txt
is the latest version for 'data', image_10.txt
is the only file for 'image', and report_8.txt
is the latest for 'report'.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer
n
representing the number of filenames. - The following
n
lines each contain a filename, following the formatbase_version.txt
.
outputFormat
Output to stdout a single line containing the most recent file for each unique base. The filenames should be printed in a single line separated by a single space, and they must be ordered lexicographically by their base names.## sample
6
data_5.txt
report_2.txt
data_3.txt
image_10.txt
data_7.txt
report_8.txt
data_7.txt image_10.txt report_8.txt