/
dwite200412p5.java
52 lines (40 loc) · 1.27 KB
/
dwite200412p5.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
/*
* DWITE programming contest solutions
* December 2004 - Problem 5: "Hidden Geography"
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/dwite-programming-contest-solutions
* https://github.com/nayuki/DWITE-programming-contest-solutions
*/
public final class dwite200412p5 extends DwiteSolution {
public static void main(String[] args) {
new dwite200412p5().run("DATA51.txt", "OUT51.txt");
}
private static final String[] PROVINCES = {
"British Columbia", "Alberta", "Saskatchewan", "Manitoba", "Ontario",
"Quebec", "Nova Scotia", "Newfoundland", "New Brunswick", "Prince Edward Island"
};
protected void runOnce() {
// Read input
String line = normalize(io.readLine());
// Find province at lowest index
int minIndex = -1;
String minProvince = null;
for (String prov : PROVINCES) {
int index = line.indexOf(normalize(prov));
if (index != -1 && (minIndex == -1 || index < minIndex)) {
minIndex = index;
minProvince = prov;
}
}
// Write output
if (minIndex != -1)
io.println(minProvince);
else
io.println("NO PROVINCE FOUND");
}
// Converts to lowercase and strips all non-letters
private static String normalize(String s) {
return s.toLowerCase().replaceAll("[^a-z]", "");
}
}