![[백준 / BOJ] 9375번 패션왕 신해빈 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbGzVxO%2FbtrJqJUNI8l%2F6pJIXDQRSZ25KXw6sn5YjK%2Fimg.png)
[백준 / BOJ] 9375번 패션왕 신해빈 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 12. 18:53
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/9375
9375번: 패션왕 신해빈
첫 번째 테스트 케이스는 headgear에 해당하는 의상이 hat, turban이며 eyewear에 해당하는 의상이 sunglasses이므로 (hat), (turban), (sunglasses), (hat,sunglasses), (turban,sunglasses)로 총 5가지 이다.
www.acmicpc.net
문제

문제 풀이
풀이는 이 분이 간단하게 잘 말해주셨다. 중고등학생 때 많이 하던 수학 문제다.
구현을 하는 법은 이전에 해왔던 대로 C++은 map을 활용해서, 파이썬은 dict나 Collections를 사용하면 된다. 아래의 코드는 Counter을 사용했는데, dict를 사용하면 values에 배열을 넣고 카운트하면 된다.
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <map> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int T; | |
std::cin >> T; | |
std::map<std::string, int> mp; | |
for (int t = 0; t < T; ++t) { | |
int N, res = 1; | |
std::string s1, s2; | |
std::cin >> N; | |
for (int i = 0; i < N; ++i) { | |
std::cin >> s1 >> s2; | |
mp[s2]++; | |
} | |
for (auto it : mp) { | |
res = res * (it.second + 1); | |
} | |
std::cout << res - 1 << "\n"; | |
mp.clear(); | |
} | |
return 0; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import Counter | |
input = sys.stdin.readline | |
T = int(input()) | |
for _ in range(T): | |
N = int(input()) | |
arr = [] | |
res = 1 | |
for i in range(N): | |
s1, s2 = input().split() | |
arr.append(s2) | |
arr = Counter(arr) | |
for key in arr: | |
res *= arr[key] + 1 | |
print(res - 1) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 2004번 조합 0의 개수 (C++, Python) (0) | 2022.06.20 |
---|---|
[백준 / BOJ] 1676번 팩토리얼 0의 개수 (C++, Python) (0) | 2022.06.13 |
[백준 / BOJ] 1010번 다리 놓기 (C++, Python) (0) | 2022.06.12 |
[백준 / BOJ] 11051번 이항 계수 2 (C++, Python) (0) | 2022.06.10 |
[백준 / BOJ] 11050번 이항 계수 1 (C++, Python) (0) | 2022.06.10 |
@Reo :: 코드 아카이브
자기계발 블로그