반응형
문제
https://www.acmicpc.net/problem/15723
JAVA코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
private static int n, m;
private static Map<Character, Character> map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
map = new HashMap<Character, Character>();
for (int idx = 0; idx < n; idx++) {
st = new StringTokenizer(br.readLine());
char key = st.nextToken().charAt(0);
st.nextToken();
char val = st.nextToken().charAt(0);
map.put(key, val);
}
st = new StringTokenizer(br.readLine());
m = Integer.parseInt(st.nextToken());
for (int idx = 0; idx < m; idx++) {
st = new StringTokenizer(br.readLine());
char key = st.nextToken().charAt(0);
st.nextToken();
char val = st.nextToken().charAt(0);
System.out.println(find(key, val) ? "T" : "F");
}
}
public static boolean find(char key, char target) {
boolean ret = false;
if (!map.containsKey(key) || (map.get(key).charValue() == key)) {
return false;
} else if (map.get(key).charValue() == target) {
return true;
} else {
ret = find((map.get(key).charValue()), target);
}
return ret;
}
}
반응형
'Algorithm > 문제 풀이' 카테고리의 다른 글
[BAEKJOON_1446 - JAVA] 지름길 (0) | 2023.06.16 |
---|---|
[BAEKJOON_14503 -JAVA] 로봇 청소기 (0) | 2023.06.16 |
[BAEKJOON_11726 - JAVA] 2×n 타일링 (0) | 2022.12.07 |
[BAEKJOON_17615 - JAVA] 볼 모으기 (0) | 2022.12.07 |
[BAEKJOON_12865 - JAVA] 평범한 배낭 (0) | 2022.11.24 |