-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrack.java
61 lines (46 loc) · 1.49 KB
/
Crack.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
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Crack
{
public static long maxPath(List<Integer> words) {
if (words.size() == 0) {
return 0L;
}
else if (words.size() == 1) {
return words.get(0);
}
else if (words.size() == 2) {
return max( words.get(0), words.get(1));
}
long[] cache = new long[ words.size() ];
cache[words.size() - 1] = words.get(words.size() -1);
cache[words.size() - 2] = words.get(words.size() -2);
long max = max( cache[words.size() - 1], cache[words.size() - 2] );
for (int j = words.size() - 3; j >=0; j--) {
long x = words.get(j) + cache[j + 2];
long y = words.get(j);
if (j + 3 < words.size()) {
y += cache[j+3];
}
cache[j] = max(x,y);
if (cache[j] > max) {
max = cache[j];
}
}
return max;
}
private static long max(long a, long b) {
return a > b ? a : b;
}
public static void main(String[] argv) {
Scanner in = new Scanner(System.in).useDelimiter("[^a-zA-Z]+");
PrintWriter out = new PrintWriter(System.out);
List<Integer> words = new ArrayList<>();
while (in.hasNext()) {
String s = in.next();
words.add(s.length());
}
System.out.println(""+maxPath(words));
}
}