-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_20191.java
More file actions
63 lines (51 loc) · 1.9 KB
/
BOJ_20191.java
File metadata and controls
63 lines (51 loc) · 1.9 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
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.util.*;
public class BOJ_20191 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String t = br.readLine();
Map<Character, List<Integer>> map = new HashMap<>();
// t 저장
for (int i = 0; i < t.length(); i++) {
Character word = t.charAt(i);
map.putIfAbsent(word, new ArrayList<>());
map.get(word).add(i);
}
// // 확인용 출력
// for (Map.Entry<Character, List<Integer>> entry : map.entrySet()) {
// System.out.println(entry.getKey() + " -> " + entry.getValue());
// }
int result = 1;
int curIdx = -1; // 전역으로 현재 위치 저장
for(int i = 0; i < s.length(); i++) {
Character target = s.charAt(i);
// 단어가 없는 경우
if (!map.containsKey(target)) {
System.out.println(-1);
return;
}
List<Integer> targetIdx = map.get(target);
System.out.println("target 단어: " + target + ", 해당 단어의 위치 인덱스: " + targetIdx + ", 지금 내 위치: " + curIdx);
// 이분 탐색으로 curIdx 업데이트
int left = 0;
int right = targetIdx.size();
while(left < right) {
int mid = (left + right) / 2;
if(targetIdx.get(mid) > curIdx) {
right = mid;
} else {
left = mid + 1;
}
}
int pos = left;
if (pos == targetIdx.size()) {
result++;
curIdx = targetIdx.get(0);
} else {
curIdx = targetIdx.get(pos);
}
}
System.out.println(result);
}
}