Skip to content

Commit 60797a1

Browse files
authored
Merge pull request #203 from AlgorithmWithGod/khj20006
[20250305] BOJ / S1 / BOJ 거리 / 권혁준
2 parents 9c1063b + 5ef8df0 commit 60797a1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st;
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static int nextInt() {return Integer.parseInt(st.nextToken());}
15+
static long nextLong() {return Long.parseLong(st.nextToken());}
16+
static void bwEnd() throws Exception {bw.flush();bw.close();}
17+
18+
// Additional field
19+
20+
static int N;
21+
static int[] dp;
22+
static char[] A;
23+
static final int INF = (int)1e9;
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
ready();
28+
solve();
29+
30+
bwEnd();
31+
32+
}
33+
34+
static void ready() throws Exception{
35+
36+
N = Integer.parseInt(br.readLine());
37+
A = br.readLine().toCharArray();
38+
dp = new int[N];
39+
Arrays.fill(dp, INF);
40+
41+
}
42+
43+
static void solve() throws Exception{
44+
45+
dp[0] = 0;
46+
for(int i=1;i<N;i++) {
47+
char tar;
48+
if(A[i] == 'B') tar = 'J';
49+
else if(A[i] == 'O') tar = 'B';
50+
else tar = 'O';
51+
52+
for(int j=0;j<i;j++) if(A[j] == tar) {
53+
dp[i] = Math.min(dp[i], dp[j] + (i-j)*(i-j));
54+
}
55+
}
56+
bw.write((dp[N-1] == INF ? -1 : dp[N-1]) + "\n");
57+
58+
}
59+
60+
}
61+
62+
```

0 commit comments

Comments
 (0)