Algorithm
[해시] 백준-9375 패션왕 신해빈 실버3 (Java)
차노도리
2023. 4. 8. 12:41
백준-9375 : 패션왕 신해빈
https://www.acmicpc.net/problem/9375
9375번: 패션왕 신해빈
첫 번째 테스트 케이스는 headgear에 해당하는 의상이 hat, turban이며 eyewear에 해당하는 의상이 sunglasses이므로 (hat), (turban), (sunglasses), (hat,sunglasses), (turban,sunglasses)로 총 5가지 이다.
www.acmicpc.net
풀의
- 입력받은 옷을 종류별로 카운트
- 조합을 이용하여 입을수 있는 방법의 수를 구한다(안 입는 경우는 제외)
코드 (java)
import java.util.HashMap;
import java.util.Scanner;
public class Baejoon9375 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
if (m == 0) {
System.out.println("0");
continue;
}
int result = 1;
HashMap<String, Integer> clothesMap = new HashMap<>();
sc.nextLine(); // 버퍼 비우기
for (int j = 0; j < m; j++) {
String[] clothesInfo = sc.nextLine().split(" ");
clothesMap.put(clothesInfo[1], clothesMap.getOrDefault(clothesInfo[1], 0) + 1);
}
for (String key : clothesMap.keySet()) {
int tempInt = clothesMap.get(key);
result *= tempInt + 1;
}
System.out.println(result - 1);
}
}
}