-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW05_4111056036_5.java
191 lines (156 loc) · 5.26 KB
/
HW05_4111056036_5.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
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
public class HW05_4111056036_5 extends WordChain {
class HashMap<K, V> {
private static final int DEFAULT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private Node<K, V>[] table;
private int size;
@SuppressWarnings("unchecked")
public HashMap() {
this.table = new Node[DEFAULT_CAPACITY];
}
@SuppressWarnings("unchecked")
private void grew() {
if ((size + 1) == (table.length * DEFAULT_LOAD_FACTOR)) {
Node<K, V>[] oldTable = table;
table = new Node[table.length << 1];
size = 0;
for (int i = 0; i < oldTable.length; ++i) {
Node<K, V> node = oldTable[i];
while (node != null) {
put(node.key, node.value);
node = node.next;
}
}
}
}
public void put(K key, V value) {
grew();
int hash = hash(key);
int index = calcIndex(hash);
Node<K, V> node = table[index];
Node<K, V> newNode = new Node<>(key, value, null);
if (node == null) {
table[index] = newNode;
} else {
boolean keyRepeat = false;
Node<K, V> last = null;
while (node != null) {
if (node.key.equals(key)) {
keyRepeat = true;
node.value = value;
break;
} else {
last = node;
node = node.next;
}
}
if (!keyRepeat) {
last.next = newNode;
}
}
++size;
}
public V get(K key) {
int hash = hash(key);
int index = calcIndex(hash);
Node<K, V> node = table[index];
while (node != null) {
if (node.key.equals(key)) {
return node.value;
} else {
node = node.next;
}
}
return null;
}
public int size() {
return size;
}
private int hash(Object key) {
if (key == null) {
return 0;
}
int h = key.hashCode();
h = h ^ (h >>> 16);
return h;
}
private int calcIndex(int hash) {
return (table.length - 1) & hash;
}
public V getOrDefault(K key, V defaultValue) {
V value = get(key);
return (value != null) ? value : defaultValue;
}
private class Node<E, A> {
E key;
A value;
Node<E, A> next;
public Node(E key, A value, Node<E, A> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
}
static class Node {
String word;
double frequency;
Node next;
Node(String word, double frequency) {
this.word = word;
this.frequency = frequency;
}
public void connect(String word) {
frequency += 1;
Node currentNode = next;
while (currentNode != null) {
if (currentNode.word.equals(word)) {
currentNode.frequency += 1;
return;
}
currentNode = currentNode.next;
}
Node newNode = new Node(word, 1);
newNode.next = next;
next = newNode;
}
}
HashMap<String, Node> nodes = new HashMap<>();
String sequence, initialSequence;
int sequenceLength;
String[] path = new String[4];
double maxProbability = 0;
public void depthFirstSearch(String current, double probability, int depth) {
if (depth == 0) {
if (probability > maxProbability) {
maxProbability = probability;
}
return;
}
Node sourceNode = nodes.get(current);
if (sourceNode == null) {
sourceNode = new Node(current, 0);
nodes.put(current, sourceNode);
for (int i = sequence.indexOf(current); i != -1 && i + 5 <= sequenceLength; i = sequence.indexOf(current, i + 1)) {
sourceNode.connect(sequence.substring(i + 3, i + 5));
}
}
for (Node next = sourceNode.next; next != null; next = next.next) {
double nextProbability = probability * next.frequency / sourceNode.frequency;
if (nextProbability <= maxProbability) {
continue;
}
path[4 - depth] = next.word;
depthFirstSearch(next.word, nextProbability, depth - 1);
}
}
@Override
public String sequenceProbability(String[] A) {
initialSequence = A[0];
sequence = " " + A[1];
sequenceLength = sequence.length();
path[0] = initialSequence;
depthFirstSearch(initialSequence, 1, 3);
return String.join(" ", path);
}
}