(9375)-패션왕 신해빈[C++]

image

풀어 보기

문제

해빈이는 패션에 매우 민감해서 한번 입었던 옷들의 조합을 절대 다시 입지 않는다. 예를 들어 오늘 해빈이가 안경, 코트, 상의, 신발을 입었다면, 다음날은 바지를 추가로 입거나 안경대신 렌즈를 착용하거나 해야한다. 해빈이가 가진 의상들이 주어졌을때 과연 해빈이는 알몸이 아닌 상태로 며칠동안 밖에 돌아다닐 수 있을까?

입력

첫째 줄에 테스트 케이스가 주어진다. 테스트 케이스는 최대 100이다.

각 테스트 케이스의 첫째 줄에는 해빈이가 가진 의상의 수 n(0 ≤ n ≤ 30)이 주어진다. 다음 n개에는 해빈이가 가진 의상의 이름과 의상의 종류가 공백으로 구분되어 주어진다. 같은 종류의 의상은 하나만 입을 수 있다. 모든 문자열은 1이상 20이하의 알파벳 소문자로 이루어져있으며 같은 이름을 가진 의상은 존재하지 않는다.

출력

각 테스트 케이스에 대해 해빈이가 알몸이 아닌 상태로 의상을 입을 수 있는 경우를 출력하시오.

풀이 전략

value값에 set을 이용한 풀이와 int를 넣은 풀이 둘다 해결하였다. 처음에 모든 경우의 수를 고려하지 않아서 틀리게 나왔었다.

핵심은 answer *= (it->second)+1; 이부분 알몸인 경우의 수 한가지 빼준것 answer=answer-1; 상당히 수학적인 접근이 필요한 것 같다.


소스코드-set

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
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;	
	
	for(int i=0; i<T; i++){
		int n;
		cin >> n;
		int answer=1;
		if(n==0) answer=0; //n이 0일때는 옷입는경우의 수 당연히 0
		else {
			map<string, int> m;
			
			for(int i=0; i<n; i++){
				string clothe,clothetype;
				cin >> clothe >> clothetype;
        		if(m.find(clothetype) != m.end()){          //이미 맵에 해당 종류 있으면
        		    m[clothetype]++;                     //그 종류의 가짓수(value) 1 증가
        		}
       			else{
           			m[clothetype] = 1;
        		    // m.insert(make_pair(clothetype,1));         //없으면 1로 초기화해서 추가
        		}				
		}
	
              	
        	for(auto it=m.begin(); it!=m.end(); it++){
        		answer *= (it->second)+1;
//        	key  value
//			1     3
//			2     2
//			3     1
//        	-> (3+1) * (2+1) * (1*1) -> 24
//        	24 -1     //아무것도 안입는 경우(알몸) 인경우 빼줘야됨 
			}
	    	answer=answer-1;
		} 	
		cout << answer << "\n";				
	}
}


소스코드-int

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
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> T;	
	
	for(int i=0; i<T; i++){
		int n;
		cin >> n;
		int answer=1;
		if(n==0) answer=0; //n이 0일때는 옷입는경우의 수 당연히 0
		else {
			map<string, int> m;
			
			for(int i=0; i<n; i++){
				string clothe,clothetype;
				cin >> clothe >> clothetype;
        		if(m.find(clothetype) != m.end()){          //이미 맵에 해당 종류 있으면
        		    m[clothetype]++;                     //그 종류의 가짓수(value) 1 증가
        		}
       			else{
           			m[clothetype] = 1;
        		    // m.insert(make_pair(clothetype,1));         //없으면 1로 초기화해서 추가
        		}				
		}
	
              	
        	for(auto it=m.begin(); it!=m.end(); it++){
        		answer *= (it->second)+1;
//        	key  value
//			1     3
//			2     2
//			3     1
//        	-> (3+1) * (2+1) * (1*1) -> 24
//        	24 -1     //아무것도 안입는 경우(알몸) 인경우 빼줘야됨 
			}
	    	answer=answer-1;
		} 
		
		cout << answer << "\n";				
	}


}
0%