-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiltration.java
More file actions
executable file
·196 lines (170 loc) · 5.79 KB
/
Infiltration.java
File metadata and controls
executable file
·196 lines (170 loc) · 5.79 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
public class Infiltration{
public static final String[][] dictionary = new String[][]{
{},{},
{"be"},
{"our", "rum"},
{"will", "dead", "hook", "ship"},
{"blood", "sable"},
{"avenge", "parrot"},
{"captain"},
{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}
};
public static String[] input;
public static int uniqueChars;
public static HashSet<SubstitutionMap> cache;
public static SubstitutionMap solution;
public static void main(String[] args) throws IOException {
String[] msg = null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))){
String line = in.readLine();
if (!line.isEmpty())
msg = line.split(" ");
}
SubstitutionMap solution = breakDecrypt(msg);
if (solution != null){
System.out.println(solution.decrypt(input));
} else {
System.out.println("Impossible");
}
}
public static final SubstitutionMap breakDecrypt(String[] input){
if (input == null || input.length == 0)
return null;
try {
solution = null;
Infiltration.input = input;
cache = new HashSet<>();
uniqueChars = countUniqueChars();
breakDecrypt(0, new SubstitutionMap());
} catch (IllegalStateException e) {}
return solution;
}
public static int countUniqueChars(){
boolean[] charMap = new boolean[26];
int count = 0;
for(String word : input){
for(int i=0; i<word.length(); ++i){
if (!charMap[word.charAt(i) - 'a']){
++count;
charMap[word.charAt(i) - 'a'] = true;
}
}
}
return count;
}
public static final void breakDecrypt(int inputIndex, SubstitutionMap map){
if (cache.contains(map)) // already checked?
return;
if (inputIndex >= input.length) { // The end?
if (map.size == uniqueChars){
if (solution != null && !solution.equals(map)) {
solution = null;
throw new IllegalStateException("Second solution found");
}
solution = map;
}
cache.add(map);
return;
}
for (String dictionaryWord : dictionary[input[inputIndex].length()]) {
if (map.keyValidForWord(input[inputIndex], dictionaryWord)) {
SubstitutionMap mapCopy = map.copy();
mapCopy.applywordToMap(input[inputIndex], dictionaryWord);
breakDecrypt(inputIndex+1, mapCopy);
}
}
// skip word
breakDecrypt(inputIndex+1, map);
}
public static class SubstitutionMap{
private char[] decodeMap = new char[26]; // [enc] -> clear
private char[] encodeMap = new char[26]; // [clear] -> enc
private int size;
private int hashCode = 0;
// we are running on one thread so we might be able to save time by declaring the data as static
static char[] tmpDecodeMap = new char[26];
static char[] tmpEncodeMap = new char[26];
public boolean keyValidForWord(String enc, String word){
for (int i = 0; i < 26; i++) {
tmpDecodeMap[i] = 0;
tmpEncodeMap[i] = 0;
}
for (int i=0; i<enc.length(); ++i) {
char c = decodeMap[enc.charAt(i) - 'a'];
char d = encodeMap[word.charAt(i) - 'a'];
char y = tmpDecodeMap[enc.charAt(i) - 'a'];
char z = tmpEncodeMap[word.charAt(i) - 'a'];
if (c != 0 && c != word.charAt(i)) // is there already a decoding mapping
return false;
else if (d != 0 && d != enc.charAt(i)) // is there already a encoding mapping
return false;
else if (y != 0 && y != word.charAt(i)) // do we have two enc letters pointing to same clear letter
return false;
else if (z != 0 && z != enc.charAt(i)) // do we have two clear letters pointing to same enc letter
return false;
tmpDecodeMap[enc.charAt(i) - 'a'] = word.charAt(i);
tmpEncodeMap[word.charAt(i) - 'a'] = enc.charAt(i);
}
return true;
}
public void applywordToMap(String enc, String word){
for (int i=0; i<enc.length(); ++i) {
if (decodeMap[enc.charAt(i) - 'a'] == 0)
++size;
decodeMap[enc.charAt(i) - 'a'] = word.charAt(i);
encodeMap[word.charAt(i) - 'a'] = enc.charAt(i);
}
hashCode = -1;
}
public SubstitutionMap copy(){
SubstitutionMap copy = new SubstitutionMap();
System.arraycopy(decodeMap, 0, copy.decodeMap, 0, decodeMap.length);
System.arraycopy(encodeMap, 0, copy.encodeMap, 0, encodeMap.length);
copy.size = size;
copy.hashCode = hashCode;
return copy;
}
public char encode(char raw){
return encodeMap[raw - 'a'];
}
public char decode(char enc){
return decodeMap[enc - 'a'];
}
public int size(){
return size;
}
public void print(){
for (int i=0; i<decodeMap.length; ++i) {
System.out.print((char)(i+97) +"="+decodeMap[i]+" ");
}
System.out.println();
}
public String decrypt(String[] encrypted){
StringBuilder str = new StringBuilder(100);
for (int k = 0; k < encrypted.length; k++) {
String word = encrypted[k];
if (k != 0) str.append(' ');
for (int i = 0; i < word.length(); ++i) {
char c = decodeMap[word.charAt(i) - 'a'];
str.append((c == 0 ? '?' : c));
}
}
return str.toString();
}
public int hashCode(){
if (hashCode < 0)
hashCode = Arrays.hashCode(decodeMap);
return hashCode;
}
public boolean equals(Object o){
if (o instanceof SubstitutionMap)
return Arrays.equals(decodeMap, ((SubstitutionMap) o).decodeMap);
return false;
}
}
}