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 | #include <stdio.h> #include <map> #include <string> #include <iostream> using namespace std; map <string, int> m; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { string str; cin >> str; if (m.find(str) == m.end()) { // 이렇게 한다는 것 자체가 map 함수의 구성요소 하나를 insert 하는 것과 같다 m[str] = 1; } else { m[str]++; } } for (int i = 0; i < n - 1; i++) { string str; cin >> str; if (m[str] == 1) { m.erase(str); } else if (m[str] > 1) { m[str]--; } } string rst = m.begin()->first; cout << rst; } | cs |
map 헤더를 처음 써봤다.
string 을 인덱스로 사용하는 경우에 유용한 방식이다.
자료구조로는 레드블랙 트리를 사용한다고 한다.
map 에서, insert 를 굳이 하지 않아도 m[idx]=1; 같은 연산으로 insert 가 된다.
'알고리즘 > 문자와 문자열' 카테고리의 다른 글
1371 가장 많은 글자 (0) | 2020.01.05 |
---|---|
1764 듣보잡 (0) | 2020.01.05 |
2857 FBI (0) | 2020.01.04 |
2810 컵홀더 (0) | 2020.01.04 |
5598 카이사르 암호 (0) | 2020.01.04 |