-
Notifications
You must be signed in to change notification settings - Fork 19
/
deduplicatingfiles.java
63 lines (47 loc) · 1.38 KB
/
deduplicatingfiles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
import java.io.*;
public class deduplicatingfiles {
public static char hash(String str) {
char hash = (char)0;
for (int i = 0; i < str.length(); i++)
hash ^= str.charAt(i);
return hash;
}
public static int triangle(int n) {
return (n)*(n + 1) / 2 - n;
}
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
int files = Integer.parseInt(scan.readLine());
if (files == 0)
break;
HashMap<String , Integer> uniques = new HashMap<>();
while (files --> 0)
{
String file = scan.readLine();
if (!uniques.containsKey(file))
uniques.put(file , 1);
else
uniques.put(file , uniques.get(file) + 1);
}
int collisions = 0;
for (String file1 : uniques.keySet())
{
char hash1 = hash(file1);
for (String file2 : uniques.keySet())
{
if (!file1.equals(file2))
{
char hash2 = hash(file2);
if (hash1 == hash2)
collisions += uniques.get(file1)*uniques.get(file2);
}
}
}
System.out.println(uniques.size() + " " + collisions / 2);
}
scan.close();
}
}