-
Notifications
You must be signed in to change notification settings - Fork 2
/
CodeTheft.java
94 lines (76 loc) · 3.03 KB
/
CodeTheft.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Problem: https://open.kattis.com/problems/codetheft
Author: Adrian Reithaug
Submitted: May 27th, 2017
Time: 1.45s / 3.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class CodeTheft {
static List<List<Long>> fragments = new ArrayList<>();
static Map<Integer, Integer> lengths = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
final String end = "***END***";
int maxLength = 0;
int numFragments = Integer.parseInt(reader.readLine());
String[] names = new String[numFragments];
for (int i = 0; i <= 10000; i++) {
fragments.add(i, new ArrayList<>());
}
for (int i = 0; i < numFragments; i++) {
names[i] = reader.readLine();
for (String line = reader.readLine(); !line.equals(end); line = reader.readLine()) {
addHash(i, line);
}
}
for (String line = reader.readLine(); !line.equals(end); line = reader.readLine()) {
addHash(numFragments, line);
}
int repositoryLength = fragments.get(numFragments).size();
for (int repoLine = 0; repoLine < repositoryLength; repoLine++) {
if (repositoryLength - repoLine < maxLength) {
break;
}
for (int fragName = 0; fragName < numFragments; fragName++) {
int fragmentLength = fragments.get(fragName).size();
for (int fragLine = 0; fragLine < fragmentLength; fragLine++) {
int thisLength = 0;
for (int line = 0; line < fragmentLength; line++) {
if (repoLine + line < repositoryLength && fragLine + line < fragmentLength && Objects.equals(fragments.get(fragName).get(fragLine + line), fragments.get(numFragments).get(repoLine + line))) {
thisLength++;
} else {
break;
}
}
if (thisLength >= maxLength) {
maxLength = thisLength;
lengths.put(fragName, maxLength);
}
}
}
}
System.out.print(maxLength);
if (maxLength != 0) {
for (int fragName = 0; fragName < numFragments; fragName++) {
if (lengths.getOrDefault(fragName, -1) == maxLength) {
System.out.print(" " + names[fragName]);
}
}
}
System.out.println("");
}
public static void addHash(int pos, String line) {
line = line.trim();
if (!line.isEmpty()) {
long hashCode = line.replaceAll("\\s+", " ").hashCode();
fragments.get(pos).add(hashCode);
}
}
}