https://www.acmicpc.net/problem/9375
조건에 맞게 입력을 받아 조합을 계산하는 문제. 중복되는 의상(같은 이름의 의상)이 들어오지 않는다고 했으므로, 어느 카테고리에 속하는 의상인지만 확인해서 해당 카테고리의 값을 +1만 하면 된다.
쉬운 문제인데 다른 풀이 아이디어가 나이스해서 적어본다 ~
처음에 풀때 조합을 재귀로 포함하는경우/포함하지 않는 경우 나눠서 재귀로 구현했는데
아예 아무것도 선택하지 않은 경우를 종류에 포함해서(기존 의상 값+1), 모든 배열 값을 곱한 후 아무것도 입지 않은 경우의 수 1만 빼면 훨씬 쉽게 구현 가능했다 굿~😊
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
int T,n;
string str1, str2;
int arr[31];
int main(){
cin>>T;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(T--){
memset(arr, 0, sizeof(arr));
map<string, int> m;
int ans=1;
cin>>n;
for(int i=0; i<n; i++) {
cin>>str1>>str2;
if(m.find(str2)==m.end()){
m.insert({str2,1});
}
m[str2]++;
}
for(auto elem :m) ans*= elem.second ;
ans--;
cout<<ans<<'\n';
}
}