알고리즘&자료구조/문제

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

Nakuri 2023. 2. 3. 14:20
728x90

개요

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

 

9375번: 패션왕 신해빈

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

www.acmicpc.net

실버급 수학 및 해시 문제다.

풀이

경우의 수를 구할 식만 떠올리면 된다.

분류별 옷의 수 + 1(벗은 상태)를 전부 곱해주면 된다.

주의할 점은 알몸인 상태는 제외라고 나와있으니 이 결과에 -1을 해주어야 한다.

map을 사용해 구현했다.

코드

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

void solve()
{
	map<string, int> m{};
	map<string, int>::iterator iter;
	string clothes, type;
	int cnt = 1;
	int n;
	cin >> n;

	for (int i = 0; i < n; i++) 
	{
		cin >> clothes >> type;
		m[type]++;
	}
	for (iter = m.begin(); iter != m.end(); iter++)
		cnt *= iter->second+1;

	cout << --cnt << "\n";
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
		solve();
}

'알고리즘&자료구조 > 문제' 카테고리의 다른 글

[BOJ] 2146 다리 만들기 C++  (1) 2023.02.04
[BOJ] 2667 단지번호붙이기 C++  (3) 2023.02.04
[BOJ] 2178 미로탐색 C++  (0) 2023.02.03
[BOJ] 2644 촌수계산 C++  (0) 2023.02.03
[BOJ] 2309 일곱 난쟁이 C++  (0) 2023.02.02