-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdictionaryattack.java
More file actions
89 lines (65 loc) · 1.67 KB
/
dictionaryattack.java
File metadata and controls
89 lines (65 loc) · 1.67 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
import java.util.regex.*;
import java.util.*;
public class dictionaryattack {
// This problem has since been rejudged
// This solution will now give you TLE
public static void addTransposes(HashMap<String , Integer> dictionary) {
for (int changes = 1; changes <= 3; changes++)
{
HashMap<String , Integer> queue = new HashMap<>();
for (String word: dictionary.keySet())
{
char[] t = word.toCharArray();
// Transposes of "Hello" : "eHllo" , "Hlelo" , "Hello" (Repeated) , "Helol"
for (int i = 0; i < t.length - 1; i++)
{
char temp = t[i];
t[i] = t[i + 1];
t[i + 1] = temp;
String transpose = new String(t);
if (!dictionary.containsKey(transpose))
queue.put(transpose , changes);
temp = t[i];
t[i] = t[i + 1];
t[i + 1] = temp;
}
}
dictionary.putAll(queue);
}
}
public static int countDigits(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++)
{
char x = str.charAt(i);
if (x >= '0' && x <= '9')
count++;
}
return count;
}
public static void main(String[] args) {
Kattio scan = new Kattio(System.in);
int N = scan.getInt();
HashMap<String , Integer> dictionary = new HashMap<>();
for (int i = 0; i < N; i++)
dictionary.put(scan.getWord() , 0);
addTransposes(dictionary);
CandidateLoop:
while (scan.hasMoreTokens())
{
String candidate = scan.getWord();
String candidateRegex = candidate.replaceAll("[0-9]" , "[a-zA-Z]");
int changes = countDigits(candidate);
for (String word : dictionary.keySet())
{
if (Pattern.matches(candidateRegex , word))
{
if (changes + dictionary.get(word) <= 3)
continue CandidateLoop;
}
}
System.out.println(candidate);
}
scan.close();
}
}