Skip to content

Commit 93c817c

Browse files
authored
[20251107] BOJ / G2 / 동전 옮기기 / 이종환
1 parent 434a4be commit 93c817c

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
import java.util.StringTokenizer;
11+
12+
public class Main {
13+
14+
static int len,idx1,idx2;
15+
static boolean[][] dp;
16+
static char[] arr,comp,move = new char[2];
17+
static char last = '1';
18+
static String ans;
19+
static Queue<Character> arrQ = new LinkedList<>();
20+
static Queue<Character> fingerQ = new LinkedList<>();
21+
22+
public static void main(String[] args) throws IOException {
23+
init();
24+
process();
25+
print();
26+
}
27+
28+
private static void init() throws IOException{
29+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));;
30+
len = Integer.parseInt(br.readLine());
31+
char[] temp = br.readLine().toCharArray();
32+
arr = br.readLine().toCharArray();
33+
34+
StringTokenizer st = new StringTokenizer(br.readLine());
35+
idx1 = Integer.parseInt(st.nextToken());
36+
idx2 = Integer.parseInt(st.nextToken());
37+
38+
int idx = 0;
39+
int moveIdx = 0;
40+
41+
comp = new char[len-2];
42+
43+
for (int i = 0; i < len; i++) {
44+
if (i == idx1 || i == idx2) {
45+
move[moveIdx++] = temp[i];
46+
} else {
47+
comp[idx++] =temp[i];
48+
}
49+
50+
}
51+
dp = new boolean[len][3]; // dp[i][j] move j개쓰고 이곳에 도달가능?
52+
53+
54+
}
55+
56+
57+
private static void process() {
58+
int idx = 0;
59+
int mIdx = 0;
60+
61+
62+
if (arr[0] != comp[0] && arr[0] != move[0]) {
63+
ans = "NO";
64+
return;
65+
}
66+
67+
if (arr[0] == comp[0]) {
68+
dp[0][0] = true;
69+
}
70+
if (arr[0] == move[0]) {
71+
dp[0][1] = true;
72+
}
73+
74+
75+
for (int i = 1; i < len; i++) {
76+
for (int j = 0; j < 3; j++) {
77+
78+
if (!dp[i-1][j]) continue;
79+
// 이제 비교
80+
81+
if (i-j < len-2) {
82+
if (arr[i] == comp[i-j]) {
83+
dp[i][j] = true;
84+
}
85+
}
86+
87+
if (j!= 2 && arr[i] == move[j]) {
88+
dp[i][j+1] = true;
89+
}
90+
}
91+
}
92+
93+
ans = dp[len-1][2]?"YES":"NO";
94+
}
95+
96+
97+
98+
private static void print() {
99+
System.out.println(ans);
100+
}
101+
}
102+
103+
104+
```

0 commit comments

Comments
 (0)