[BOJ] 9375번 패션왕 신해빈 - C++

"정수론 및 조합론"

Posted by Yongmin on June 16, 2021

문제

패션왕 신해빈

풀이

category 별로 갯수를 어레이에 정리한 뒤, 카테고리 안의 물건 수 + 그 카테고리 물건을 안 끼는 경우들, 즉 카테고리마다 +1(안끼는 경우의 수)를 하여, 그것들의 조합을 구하기 위해 각자 곱한다. 그러면 총 경우의 수가 나오는데, 이 때 알몸인 경우, 즉 모두 안 끼는 경우를 하나 빼주면 원하는 답의 결과가 도출된다.

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <string>
using namespace std;




int main(){
    int N;
    scanf("%d", &N);
    
    for(int i = 0; i<N; i++){
    vector<pair<string, int>> testcase;
        int M;
        scanf("%d", &M);
        
        for(int i = 0; i<M; i++){
            string a;
            string b;
            cin >> a >> b;
            
            for(int i = 0; i<=testcase.size(); i++){
                if(i == testcase.size()){
                    testcase.push_back(make_pair(b, 1));
                    break;
                }
                if(b == testcase[i].first){
                    testcase[i].second++;
                    break;
                }
            }
        }
        int ans = 1;
        
        for(int i = 0; i<testcase.size(); i++){
            ans = ans * (testcase[i].second+1);
        }
        
        printf("%d\n", ans-1);
    }
}


# # #