Post

백준 9375번, 패션왕 신혜빈

백준 9375번 c++ 문제풀이


문제:

백준 9375

풀이:

  1. <string, int> map을 선언한다.
  2. 입력받은 카테고리 별로 입력 받은 카테고리의 map의 int 값을 증가시킨다.(처음 값은 0)
  3. 조합의 문제이므로, 각각의 카테고리 수 + 1 만큼 씩 곱해주고(그 카데고리가 선택되지 않은 경우까지 포함)
  4. 마지막에 -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
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;

    cin >> t;

    for(int i=0;i<t;i++){
        map<string, int> mapset;

        int n;
        cin >> n;

        for(int j=0;j<n;j++){
            string c;  //옷
            string k;  //종류
            
            cin >> c >> k;

            mapset[k]++;
        }
        int result = 1;
        for(pair<string, int> p : mapset) {
            result *= (p.second+1);
        }
        result = result-1;
        cout << result <<"\n";
    }

    return 0;
}

감사합니다.

This post is licensed under CC BY 4.0 by the author.