problems / 277

Duplicate Uploads in the Asset Bucket

easy hashing sorting strings 1000 ms · 256 MB

Duplicate Uploads in the Asset Bucket

The web team uploads images, scripts and fonts to a shared object bucket, and over the years the same files have been uploaded under different names. The storage team exported a listing of the bucket: for every object it gives the object's path, its size in bytes and a 32-hex-digit checksum of its contents.

Two objects are duplicates of each other exactly when they have the same size and the same checksum. Objects with the same checksum but different sizes, or the same size but different checksums, are not duplicates.

Find every duplicate group: a maximal set of at least two objects that are all duplicates of each other. Inside a group, list the paths in lexicographic order. List the groups in lexicographic order of their smallest path.

The team will keep one copy from every group and delete the rest. Also report how many bytes that frees: for each group of k objects of size s, that's (k − 1)·s bytes.

Paths are compared character by character by ASCII code, and a path that is a proper prefix of another comes first. In ASCII, - < . < / < digits < _ < lowercase letters.

Input

The first line contains n (1 ≤ n ≤ 10^5), the number of objects.

Each of the next n lines describes one object as three space-separated fields: the path p, the size s and the checksum c.

  • p has 1 to 40 characters, each a lowercase letter, a digit, or one of /, ., _, -. All paths are distinct.
  • s is an integer, 0 ≤ s ≤ 10^12.
  • c is exactly 32 characters, each a digit or a lowercase letter from a to f.

Output

On the first line print g, the number of duplicate groups.

Then print g lines, one per group in the order described above. Each line lists the group's paths in lexicographic order, separated by single spaces.

On the last line print the total number of bytes freed. It can exceed 2^31, so use a 64-bit integer.

Notes

In the first example, logo.png and img/logo-old.png share size and checksum, and so do the three copies of app.js. The file img/logo-2x.png has the same checksum as the logo but a different size, so it's not a duplicate. The freed bytes are 1·4096 + 2·5000000000 = 10000004096.

In the second example every object is different, so there are no groups and nothing is freed.

Example 1

Input

7
logo.png 4096 9e107d9d372bb6826bd81d3542a419d6
js/app.js 5000000000 0cc175b9c0f1b6a831c399e269772661
img/logo-old.png 4096 9e107d9d372bb6826bd81d3542a419d6
css/site.css 812 d41d8cd98f00b204e9800998ecf8427e
v2/app.js 5000000000 0cc175b9c0f1b6a831c399e269772661
img/logo-2x.png 8192 9e107d9d372bb6826bd81d3542a419d6
backup/app.js 5000000000 0cc175b9c0f1b6a831c399e269772661

Output

2
backup/app.js js/app.js v2/app.js
img/logo-old.png logo.png
10000004096

Example 2

Input

3
fonts/inter.woff2 120000 5d41402abc4b2a76b9719d911017c592
fonts/inter.woff 120000 7d793037a0760186574b0282f2f435e7
readme.txt 0 5d41402abc4b2a76b9719d911017c592

Output

0
0

Hints · reveal one at a time if you're stuck

Hint 1

Comparing every pair of objects is slow and unnecessary. What single value could you compute for each object so that duplicates get exactly the same value?

Hint 2

Use the pair (size, checksum) as the key of a hash map whose value is the list of paths with that content. Only the lists with two or more paths matter. Mind the size of the freed-bytes total.

report an issue Prefer your own editor? sooks prep 277 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.