본문 바로가기
전공공부/코딩테스트

(c++) 프로그래머스 "튜플"

by 시아나 2022. 3. 15.

https://programmers.co.kr/learn/courses/30/lessons/64065

 

코딩테스트 연습 - 튜플

"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]

programmers.co.kr


#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

bool arange(string n1, string n2) {
    return n1.size() < n2.size();
}

vector<int> solution(string s) {
    vector<int> answer;
    vector<string> list;
    stringstream ss(s.substr(1,s.size()-2));
    string tmp;
    getline(ss, tmp, '}');
    list.push_back(tmp.substr(1, tmp.size() - 1));
    while (getline(ss, tmp, '}')) {
        list.push_back(tmp.substr(2, tmp.size() - 1));
    }
    sort(list.begin(), list.end(), arange);
    for (string str : list) {
        stringstream ss(str);
        string tmp;
        while (getline(ss, tmp, ',')) {
            if (find(answer.begin(), answer.end(), stoi(tmp)) == answer.end()) {
                answer.push_back(stoi(tmp));
            }
        }
    }
    return answer;
}