```
백준 10105번 Assigning Partners JAVA 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 10105번 풀이
https://www.acmicpc.net/problem/10105
10105번: Assigning Partners
The input consists of three lines. The first line consists of an integer N (1 < N ≤ 30), which is the number of students in the class. The second line contains the first names of the N students separated by single spaces. (Names contain only uppercase or
www.acmicpc.net
백준 10105번 Assigning Partners는
난이도 브론즈 등급의 문제로서
두줄에 이름들이 주어질 때
각 줄에 같은 인덱스에 나오는 이름이 짝을 이룬다고 할 때
각 인덱스에 나오는 이름은 동일하게 매칭 돼야 하고
같은 이름이 같은 인덱스에 짝으로 나오지 않는다고 하는 경우
예를 들어
A B C D
C D A B
는 A와 C, B와 D로 매칭이 잘되었지만
A B C D
A D B C
는 A 가 같은 열에 두 번 나오기 때문에 잘못되었고
또한 B, C, D의 짝이 각 다르기 때문에 잘못되었습니다.
매칭이 잘된 경우 good
잘못된 경우 bad를 출력하는 프로그램을 작성하면 됩니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
위아래 리스트의 값이 동일한 경우 false 처리를 하였고
해쉬 맵을 사용해서 맵에 이미 있고 해당되는 매칭이 아닌 경우 false 처리를 하였습니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int playerCount = Integer.parseInt(br.readLine());
List<String> names01 = getInputList(playerCount);
List<String> names02 = getInputList(playerCount);
String output = "bad";
if (isConsistent(playerCount, names01, names02)) {
output = "good";
}
System.out.print(output);
}
private static boolean isConsistent(int playerCount, List<String> names01, List<String> names02) {
boolean isConsistent = true;
Map<String, String> nameMap = new HashMap<>();
for (int i = 0; i < playerCount; i++) {
String name01 = names01.get(i);
String name02 = names02.get(i);
// no one is a partner of themselves
if (name01.equals(name02)) {
isConsistent = false;
break;
}
// A is a partner of B, then B is also a partner of A
if (nameMap.get(name02) != null && (!nameMap.get(name02).equals(name01))) {
isConsistent = false;
break;
}
nameMap.put(name01, name02);
}
return isConsistent;
}
private static List<String> getInputList(int playerCount) throws IOException {
List<String> inputList = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < playerCount; j++) {
inputList.add(st.nextToken());
}
return inputList;
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
'Java > Java 알고리즘' 카테고리의 다른 글
| 백준 24510번 시간복잡도를 배운 도도 JAVA 구현해보기 (0) | 2022.02.25 |
|---|---|
| 백준 15988번 1, 2, 3 더하기 3 JAVA 구현해보기 (0) | 2022.02.22 |
| 백준 1057번 토너먼트 JAVA 구현해보기 (0) | 2022.02.19 |
| 백준 10103번 주사위 게임 JAVA 구현해보기 (0) | 2022.02.19 |
| 백준 10102번 개표 JAVA 구현해보기 (0) | 2022.02.19 |