[백준] 9375 패션왕 신해빈

0

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

접근방법

먼저, 의류 이름과 종류중에서 이름은 필요없다 (종류의 경우의 수만 필요)

하나만 입는 경우도 존재하기 때문에
예제에 있는 headgear 2개, eyegear 1개 에서 1씩 더해서 곱하는 식으로 구해야함
하지만 알몸의 경우는 빼야하므로 마지막에 1을 빼줘야 한다.

풀이 (c++)

#include <bits/stdc++.h>
using namespace std; 
int t, n;
string a, b;
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
    cin>>t;
    for (int i = 0; i < t; i++)
    {
        map <string, int> _map; //map 초기화
        cin>>n;
        for (int i = 0; i < n; i++)
        {
           cin>>a>>b;
           _map[b]++; 
        }
        long long result = 1;
        for(auto c : _map){
            result *= c.second+1;
        }
        result--;
        cout<<result<<"\n";
    }
    return 0;
}

Python

import collections
t = int(input())
for i in range(t):
    n = int(input())
    l = [] 
    for j in range(n):
        a, b = input().split()
        l.append(b)
    dic = collections.Counter(l)
    result = 1
    for k in dic:
        result *= dic[k]+1
    print(result-1)

collections의 counter함수를 이용하면
리스트 내의 요소 개수를 구할 수 있다.

0개의 댓글