- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileFragmentation.java
More file actions
Latest commit
56 lines (52 loc) · 1.99 KB
/
Copy pathFileFragmentation.java
File metadata and controls
56 lines (52 loc) · 1.99 KB
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
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
importstaticjava.util.Comparator.comparing;
importstaticjava.util.stream.Collectors.toList;
classFileFragmentation {
privatestaticfinalBufferedReaderreader =
newBufferedReader(newInputStreamReader(System.in));
privatestaticbooleanfit(List<String> fragments, Stringcandidate) {
List<String> temp = newArrayList<String>(fragments);
for (inti = 1; i < candidate.length() && !temp.isEmpty(); ++i) {
finalintj = i;
temp.removeIf(x -> x.equalsIgnoreCase(candidate.substring(0, j)));
temp.removeIf(x -> x.equalsIgnoreCase(candidate.substring(j)));
}
returntemp.isEmpty();
}
privatestaticStringrestore(List<String> fragments) {
fragments.sort(comparing(String::length));
Stringlarge = fragments.get(fragments.size() - 1);
List<String> smallest = fragments.stream().filter(
x -> x.length() == fragments.get(0).length()).collect(toList());
for (Stringsmall : smallest) {
if (fit(fragments, large + small)) {
returnlarge + small;
} elseif (fit(fragments, small + large)) {
returnsmall + large;
}
}
return"Impossible";
}
publicstaticvoidmain(String[] args) throwsIOException {
intn = Integer.parseInt(reader.readLine());
reader.readLine();
for (inti = 0; i < n; ++i) {
List<String> fragments = newArrayList<String>();
do {
Strings = reader.readLine();
if (s == null || s.equalsIgnoreCase("")) {
break;
}
fragments.add(s);
} while (true);
System.out.println(restore(fragments));
if (i < n - 1) {
System.out.println();
}
}
}
}