• 문제 링크
14171번: Cities and States
To keep his cows intellectually stimulated, Farmer John has placed a large map of the USA on the wall of his barn. Since the cows spend many hours in the barn staring at this map, they start to notice several curious patterns. For example, the cities of Fl
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
Map<String, Integer> map = new HashMap<>();
int cnt = 0;
while (n-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
String key = st.nextToken().substring(0, 2);
String value = st.nextToken();
if (key.equals(value)) continue;
String concat = key.concat(value);
map.put(concat, map.getOrDefault(concat, 0) + 1);
cnt += map.getOrDefault(value.concat(key), 0);
}
bw.write(String.valueOf(cnt));
bw.flush();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 27497 알파벳 블록 - Data Structure / Java (0) | 2023.06.24 |
---|---|
[백준] 3018 캠프파이어 - Data Structure / Java (0) | 2023.06.23 |
[백준] 17393 다이나믹 롤러 - BinarySearch / Java (0) | 2023.06.21 |
[백준] 17245 서버실 - BinarySearch / Java (0) | 2023.06.20 |
[백준] 11561 징검다리 - BinarySearch / Java (0) | 2023.06.19 |
댓글