๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํŠœํ”Œ( Level 2)

by ์•ˆ์ฃผํ˜• 2021. 10. 23.

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํŠœํ”Œ

"{{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 <iostream>
#include <unordered_set>
#include <string>
#include <vector>
#include <map>
#include <algorithm>


using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    vector<pair<int,vector<string>>>v;
    map<string,int>m;
    for(int i=1; i<s.length()-1; i++){
        string temp_s;
        vector<string>temp_v;
        if(s[i]=='{'){
            while(s[i]!='}'&&i!=s.length()-1){
                if(s[i]>='0'&&s[i]<='9'&&s[i]!=' ')temp_s+=s[i];
                else{
                    temp_v.push_back(temp_s);
                    temp_s.clear();
                }
                i++;
            }
            if(temp_s.length()!=0) temp_v.push_back(temp_s);
            v.push_back({temp_v.size(),temp_v});
        }


    }

    sort(v.begin(),v.end());

    for(int i=0; i<v.size(); i++){
        for(int k=1; k<v[i].second.size(); k++){
            if(m[v[i].second[k]]==0){
                m[v[i].second[k]]++;
                answer.push_back(stoi(v[i].second[k]));
            }

        }
    }

    return answer;
}

๋Œ“๊ธ€