2023. 1. 26. 22:07ㆍ알고리즘/해시맵

https://www.acmicpc.net/problem/13915
13915번: 현수의 열기구 교실
현수는 열기구 여름특강의 강사다. 현수는 매우 성실해서 모든 수강생들의 열기구 비행을 기록하고있다. 매 비행 이후, 현수는 그 비행에 참석한 수강생들의 기록을 리스트에 추가한다. 리스트
www.acmicpc.net
풀이
중복과 정렬처리를 잘 해줘야하는 문제.
나는 중복이 안되고 자동정렬되는 set 컨테이너로 해결했다.
우선, 수강생 번호( s1 )를 문자열로 받아서 set01 에 한 글자씩 넣어주어서 중복제거와 정렬을 시켜주었다.
그 후에 s2 문자열에 set01 의 요소들을 다시 넣어주고 중복제거를 위해 set02 에 넣어주었다.
그리고 set02 사이즈를 출력하면 분류된 숙련도가 출력된다.
s1 set01 s2 set02
132 -> 1, 2, 3 -> 123 -> 123
42 -> 2, 4 -> 24 -> 24
3312 -> 1, 2, 3 -> 123 -> 123 (중복 x)
43 -> 3, 4 -> 34 -> 34
24424 -> 2, 4 -> 24 -> 123 (중복 x)
이런 느낌?
전체 코드
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int N;
set<int> set01;
set<string> set02;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while (cin >> N) {
set02.clear();
for (int i = 0; i < N; i++) {
string s1, s2;
set01.clear();
cin >> s1;
for (int j = 0; j < s1.size(); j++) {
set01.insert(s1[j]);
}
for (auto i : set01) s2 += i;
set02.insert(s2);
}
cout << set02.size() << '\n';
}
}