-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek4_Day4.java
More file actions
54 lines (46 loc) · 1.5 KB
/
Week4_Day4.java
File metadata and controls
54 lines (46 loc) · 1.5 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
package Algorithm4;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Week4_Day4 {
private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
private static final int MAX = 987654321;
private static int N;
private static char[] street;
private static int[] dp;
private static void input() throws IOException {
N = Integer.parseInt(bufferedReader.readLine());
street = new char[N];
street = bufferedReader.readLine().toCharArray();
dp = new int[N];
Arrays.fill(dp, MAX);
}
private static void solve() throws IOException {
Map<Character, Character> h = new HashMap<>();
h.put('B', 'O');
h.put('O', 'J');
h.put('J', 'B');
dp[0] = 0;
for(int i = 0; i < N-1; i++) {
int now = i;
char nowBlock = street[now];
for(int j = i +1; j < N; j++) {
int next = j;
char nextBlock = street[next];
if(h.get(nowBlock) == nextBlock) {
dp[next] = Math.min(dp[next], dp[now] + (next-now) * (next-now));
}
}
}
if(dp[N-1] == MAX) {
System.out.println("-1\n");
} else {
System.out.println(dp[N-1]);
}
}
public static void main(String[] args) throws IOException {
input();
solve();
}
}