← 返回 anthropic 的题目列表File Deduplication
类型:online_judge
Problem: File Deduplication
You are given information about files in a file system. Each file has:
a file path path
file content content
Two or more files are considered duplicates if their content strings are exactly the same.
Find all groups of duplicate files. Each returned group should contain all file paths with identical content, and only groups of size at least 2 should be returned.
Write a program that reads file information from standard input and prints all duplicate groups.
Input Format
The first line contains an integer n, the number of files.
The next n lines each contain a file path and file content separated by one space:
path content
For simplicity, assume:
path contains no spaces;
content contains no spaces;
each path appears at most once.
Output Format
Print all duplicate file groups.
Print one group per line;
Sort paths within each group in lexicographical order;
Sort groups by the first path in each group;
Separate paths by one space;
If there are no duplicate files, print nothing.
Constraints
1 <= n <= 10^5
1 <= len(path) <= 200
1 <= len(content) <= 10^4
Total input size is at most 2 * 10^6 characters.
Example 1
Input:
5
/a/1.txt hello
/a/2.txt world
/b/3.txt hello
/c/4.txt python
/d/5.txt world
Output:
/a/1.txt /b/3.txt
/a/2.txt /d/5.txt
Example 2
Input:
3
/a/a.txt x
/b/b.txt y
/c/c.txt z
Output:
Follow-up
If files are too large to fit into memory, how would you design the deduplication process? If there are many files, how would you avoid unnecessary full-content comparisons?
Example
Input
5
/a/1.txt hello
/a/2.txt world
/b/3.txt hello
/c/4.txt python
/d/5.txt world
Output
/a/1.txt /b/3.txt
/a/2.txt /d/5.txt