알고리즘

백준 9375번 패션왕 신해빈 (JAVA)

박카스마시며코딩 2022. 2. 6. 13:41

https://www.acmicpc.net/problem/9375

 

9375번: 패션왕 신해빈

첫 번째 테스트 케이스는 headgear에 해당하는 의상이 hat, turban이며 eyewear에 해당하는 의상이 sunglasses이므로   (hat), (turban), (sunglasses), (hat,sunglasses), (turban,sunglasses)로 총 5가지 이다.

www.acmicpc.net

 

저는 해당 문제를 map을 통해서 해결하였습니다. 

map을 통해 해당 종류가 몇개있는지를 판단하였습니다. 그리고 각각의 종류의 개수를 곱하였습니다.

이때 각각의 종류는 없는 것도 생각하여 +1을 하였습니다. 마지막으로 다 없는 경우도 존재하기 때문에 -1을 하였습니다.

 

package BOJ.ETC;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_9375 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        int test = stoi.apply(br.readLine());
        for(int t = 0 ; t < test ; t++){
            List<String> list = new ArrayList<>();
            Map<String,Integer> map = new HashMap<>();
            int n = stoi.apply(br.readLine());
            for(int i = 0 ; i < n ; i++){
                String[] command = br.readLine().split(" ");
                if(!list.contains(command[1])){
                    list.add(command[1]);
                }
                map.merge(command[1],2,(o1,o2)->{return o1+1;});
            }
            int result = 1;
            if(map.size() != 0){
                result = map.get(list.get(0));
            }
            for(int i = 1 ; i < list.size(); i++){
                result *= map.get(list.get(i));
            }
            System.out.println(result - 1);
        }
    }
}