Repository navigation
Expand file tree
/
Copy pathhangman.java
More file actions
executable file
·300 lines (274 loc) · 9.66 KB
/
Copy pathhangman.java
File metadata and controls
executable file
·300 lines (274 loc) · 9.66 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* (c) 2011 Laurent Vaucher
* http://www.slowfrog.com
* This file is under Apache License V2.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
/**
* Main class, because Java needs class.
*/
public class hangman {
/** Struct-like class to store the information about one test case. */
private static final class Case {
/** Index of the case. */
public int index;
/** List of dictionary words. */
public List<String> dic;
/** List of letters to be played in order. */
public List<String> lists;
public Case(int index, List<String> dic, List<String> lists) {
this.index = index;
this.dic = dic;
this.lists = lists;
}
};
/** Struct-like class to store the information about one result. */
private static final class Result {
/** Index of the case. */
public int index;
/** List of words to play for each list of letters from the test case. */
public List<String> words;
public Result(int index, List<String> words) {
this.index = index;
this.words = words;
}
/** Transforms the result into the expected output format. */
public String toString() {
String ret = "Case #" + this.index + ":";
for (String word: this.words) {
ret += " " + word;
}
return ret;
}
};
/** Main entry point. */
public static void main(String[] args) {
try {
boolean multi = false;
int workers = 0;
if (args.length > 0) {
if (args[0].equals("-w")) {
multi = true;
if (args.length > 1) {
workers = Integer.parseInt(args[1]);
}
}
}
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(r.readLine());
System.err.println("T=" + T);
Case[] cases = new Case[T];
for (int index = 1; index <= T; ++index) {
long start = System.currentTimeMillis();
String[] t = r.readLine().split("\\s");
int N = Integer.parseInt(t[0]);
int M = Integer.parseInt(t[1]);
System.err.println("#" + index + ": N=" + N + ", M=" + M);
List<String> dic = new ArrayList<String>(N);
List<String> lists = new ArrayList<String>(M);
for (int x = 0; x < N; ++x) {
dic.add(r.readLine());
}
for (int x = 0; x < M; ++x) {
lists.add(r.readLine());
}
Case c = new Case(index, dic, lists);
cases[index - 1] = c;
}
Result[] results = (multi ?
solve_with_pool(cases, workers) :
solve_without_pool(cases));
for (Result result : results) {
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** Solves the whole set of cases directly and sequentially, without resorting to other threads
* or processes.
*/
public static Result[] solve_without_pool(Case[] cases) {
Result[] results = new Result[cases.length];
for (Case c : cases) {
results[c.index - 1] = solve_case(c);
}
return results;
}
/** Solves the whole set of cases using a pool of processes. */
public static Result[] solve_with_pool(Case[] cases, int workers) {
int realworkers = (workers < 1 ? Runtime.getRuntime().availableProcessors() : workers);
ExecutorService pool = Executors.newFixedThreadPool(realworkers);
// Using Hashtable because it is synchronized
@SuppressWarnings({"unchecked"})
FutureTask<Result>[] fresults = new FutureTask[cases.length];
for (int i = 0; i < cases.length; ++i) {
final Case c = cases[i];
fresults[i] = new FutureTask<Result>(new Callable<Result>() {
public Result call() {
return solve_case(c);
}
});
pool.execute(fresults[i]);
}
pool.shutdown();
try {
pool.awaitTermination(1, TimeUnit.DAYS);
Result[] results = new Result[cases.length];
for (int i = 0; i < cases.length; ++i) {
results[i] = fresults[i].get();
}
return results;
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
return null;
}
/** Solves a full case, containing a dictionary of words and a list of lists of letters.
*
* @return the list the words to play for each list of letter.
*/
public static Result solve_case(Case c) {
List<String> words = new ArrayList<String>(c.lists.size());
for (String list : c.lists) {
words.add(solve(c.dic, list));
}
return new Result(c.index, words);
}
/** This is a struct-like class that holds temporary states of the resolution. */
private static final class Status {
/** Score (number of points lost by the player up to now) */
public int score;
/** Remaining letters in order. */
public String next_letters;
/** List of words matching the current state. */
public List<String> words;
public Status(int score, String next_letters, List<String> words) {
this.score = score;
this.next_letters = next_letters;
this.words = words;
}
};
/** Main solving function. 'dic' contains the initial dictionary of all possible words. 'lis' is
* the predefined list of letters that the player will try in order.
*
* @return the word from the dictionary that will make the player lose the most points.
*/
public static String solve(List<String> dic, String lis) {
Map<String, Integer> wordpos = new HashMap<String, Integer>();
for (int i = 0; i < dic.size(); ++i) {
wordpos.put(dic.get(i), i);
}
Map<String, Status> status = new HashMap<String, Status>();
for (String w : dic) {
String state = "__________".substring(0, w.length());
Status st = status.get(state);
if (st == null) {
List<String> tmplst = new ArrayList<String>();
tmplst.add(w);
status.put(state, new Status(0, lis, tmplst));
} else {
st.words.add(w);
}
}
int maxscore = -1;
String maxword = "";
while (status.size() > 0) {
Map<String, Status> new_status = new HashMap<String, Status>();
for (Map.Entry<String, Status> entry : status.entrySet()) {
String state = entry.getKey();
Status tmp = entry.getValue();
int score = tmp.score;
String letters = tmp.next_letters;
List<String> words = tmp.words;
if (words.size() == 1) {
if ((score > maxscore) ||
((score == maxscore) && (wordpos.get(words.get(0)) < wordpos.get(maxword)))) {
maxscore = score;
maxword = words.get(0);
}
} else {
String[] tmp2 = next_letter(words, letters);
String l = tmp2[0];
String next_letters = tmp2[1];
for (String w : words) {
String[] tmp3 = play_letter(state, w, l);
String new_state = tmp3[0];
String lose = tmp3[1];
Status st = new_status.get(new_state);
if (st == null) {
int new_score = score + (Boolean.valueOf(lose) ? 1 : 0);
List<String> tmplst = new ArrayList<String>();
tmplst.add(w);
new_status.put(new_state, new Status(new_score, next_letters, tmplst));
} else {
st.words.add(w);
}
}
}
}
status = new_status;
}
return maxword;
}
/** Knowing the set of words that are still possible, stored in 'dic', and the list of available
* letters in order, finds the next valid letter.
*
* @return a pair of values. The first one is the next letter to try. The second is the list of
* letters that will remain available after.
*/
public static String[] next_letter(List<String> dic, String lis) {
for (int i = 0; i < lis.length(); ++i) {
char l = lis.charAt(i);
if (playable(dic, l)) {
return new String[] { lis.substring(i, i + 1), lis.substring(i + 1) };
}
}
return null;
}
/** Knowing the set of words that are still possible, stored in 'dic', checks if letter 'l' is
* a valid guess.
*/
public static boolean playable(List<String> dic, char l) {
for (String w : dic) {
if (w.indexOf(l) >= 0) {
return true;
}
}
return false;
}
/** What happens when the player guesses the letter 'l' when the word is 'word' and the current
* state is 'state'.
*
* @return a pair of values. The first one is the new state, with all occurrences of 'l'
* discovered. The second one is a boolean value that is True is the player loses a point because
* he guessed wrong.
*/
public static String[] play_letter(String state, String word, String l) {
char ll = l.charAt(0);
String ret = "";
boolean lose = true;
for (int i = 0; i < state.length(); ++i) {
if (word.charAt(i) == ll) {
ret += ll;
lose = false;
} else {
ret += state.charAt(i);
}
}
return new String[] { ret, Boolean.toString(lose) };
}
}