-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptKicker.java
More file actions
153 lines (135 loc) · 4.81 KB
/
Copy pathCryptKicker.java
File metadata and controls
153 lines (135 loc) · 4.81 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
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
/**
* Problem statement can be viewed at:
* http://www.programming-challenges.com/pg.php
* ?page=downloadproblem&probid=110204&format=html
*
* The following is a solution for the above problem.
*
* @author Quinn Liu (quinnliu@vt.edu)
* @author Jason Riddle (jr1285@vt.edu)
*/
public class CryptKicker {
private static final int NUMBER_OF_UNIQUE_LETTERS = 26;
private static final int DICTIONARY_WORD_MAXIMUM_LENGTH = 16;
private static final char ASTERISK = '*';
private static List<String>[] indexedDictionary;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
String currentLine = bufferedReader.readLine();
int currentLineLength = Integer.parseInt(currentLine.trim());
String[] dictionaryOfWordsInDecryptedText = new String[currentLineLength];
for (int i = 0; i < currentLineLength; i++) {
currentLine = bufferedReader.readLine();
dictionaryOfWordsInDecryptedText[i] = currentLine;
}
indexedDictionary = (List<String>[]) new ArrayList[DICTIONARY_WORD_MAXIMUM_LENGTH + 1];
for (String dictionaryWord : dictionaryOfWordsInDecryptedText) {
int index = dictionaryWord.length();
// rearrange words within dictionary by their length for faster
// access
if (indexedDictionary[index] == null) {
indexedDictionary[index] = new ArrayList<String>();
}
indexedDictionary[index].add(dictionaryWord);
}
while ((currentLine = bufferedReader.readLine()) != null) {
String[] encryptedWords = currentLine.split(" ");
if (encryptedWords != null && encryptedWords.length != 0) {
System.out.println(decryptEncryptedWords(encryptedWords));
}
}
}
private static String decryptEncryptedWords(String[] encryptedWords) {
char[] characterMappings = new char[NUMBER_OF_UNIQUE_LETTERS];
for (int i = 0; i < characterMappings.length; i++) {
characterMappings[i] = ASTERISK;
}
characterMappings = backtrackToGetDecryptedWords(0, characterMappings,
encryptedWords);
String decryptedWords = buildDecryptedString(characterMappings,
encryptedWords);
return decryptedWords;
}
private static String buildDecryptedString(char[] characterMappings,
String[] encryptedWords) {
StringBuilder stringBuilder = new StringBuilder();
for (String encryptedWord : encryptedWords) {
char[] charArrayOfEncryptedWords = encryptedWord.toCharArray();
for (char character : charArrayOfEncryptedWords) {
if (characterMappings != null) {
stringBuilder.append(characterMappings[character - 'a']);
} else {
stringBuilder.append(ASTERISK);
}
}
stringBuilder.append(" ");
}
return stringBuilder.toString().trim();
}
private static char[] backtrackToGetDecryptedWords(int i,
char[] characterMappings, String[] encryptedWords) {
if (i == encryptedWords.length)
return characterMappings;
else {
for (String dictionaryWord : indexedDictionary[encryptedWords[i]
.length()]) {
char[] localCharacterMappings = Arrays.copyOf(
characterMappings, characterMappings.length);
if (isTheEncryptedWordsAbleToBecomeTheUnencryptedWordsBasedOnTheGivenMapping(
encryptedWords[i], dictionaryWord,
localCharacterMappings)) {
localCharacterMappings = backtrackToGetDecryptedWords(
i + 1, localCharacterMappings, encryptedWords);
if (localCharacterMappings != null) {
return localCharacterMappings;
}
}
}
return null;
}
}
public static boolean isTheEncryptedWordsAbleToBecomeTheUnencryptedWordsBasedOnTheGivenMapping(
String encryptedWords, String unencryptedWords, char[] mappings) {
if (encryptedWords.length() != unencryptedWords.length()) {
return false;
}
char[] charArrayOfEncryptedWords = encryptedWords.toCharArray();
char[] charArrayOfUnencryptedWords = unencryptedWords.toCharArray();
for (int i = 0; i < charArrayOfEncryptedWords.length; i++) {
char encryptedChar = charArrayOfEncryptedWords[i];
char plaintextChar = charArrayOfUnencryptedWords[i];
int index = encryptedChar - 'a';
if (mappings[index] != ASTERISK && mappings[index] != plaintextChar) {
return false;
} else {
mappings[index] = plaintextChar;
}
}
return isInjectiveMapping(mappings);
}
private static boolean isInjectiveMapping(char[] mappings) {
int[] characterMappings = new int[NUMBER_OF_UNIQUE_LETTERS];
for (char characterToMap : mappings) {
if (characterToMap != ASTERISK) {
int i = characterToMap - 'a';
if (characterMappings[i] != 0) {
return false;
} else {
characterMappings[i] = 1;
}
}
}
return true;
}
}