선택은 나의 것

[SWEA] 10912 외로운 문자 본문

☽ Algorithm/SWEA

[SWEA] 10912 외로운 문자

Algoribi 2021. 8. 15. 14:24

문제

SWEA 10912 : 외로운 문자

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

접근

 입력받은 문자열에서 쌍을 이루는 알파벳을 제거하고 남은 알파벳을 사전 순서대로 출력하면 되는 문제이다. map을 이용해서 알파벳들의 개수를 세주고, 그 값을 2로 나눈 나머지가 1이라면 쌍을 이루지 못한 알파벳이 있다는 뜻이므로 이를 출력해주었다. 이때, map 대신 배열을 써서 구현해도 된다.

코드

#include <iostream>
#include <map>
#define endl "\n"

using namespace std;

int main() {
    int test_case;
    cin >> test_case;
    for (int t = 1; t <= test_case; t++) {
        string s;
        cin >> s;
        map<char, int> m;
        for (char c : s) {
            if (m.find(c) == m.end())
                m.insert({c, 1});
            else
                m[c]++;
        }
        cout << "#" << t << " ";
        int chk = 0;
        for (auto it = m.begin(); it != m.end(); it++) {
            if (it->second % 2 == 0)
                continue;
            cout << it->first;
            chk++;
        }
        if (chk == 0)
            cout << "Good";
        cout << endl;
    }
}

 

깃 허브 주소 : https://github.com/algoribi/algorithm-study

Comments