본문 바로가기

Programming/Programmers

[프로그래머스/C++] 튜플(문자열)

더럽게 풀었지만 초반 풀이.

 

1) {}만 제거하고 문자열 형식으로 vector에 담는다.

ex) {{20,111},{111}} -> ["20,111","111"]

2) 문자열의 길이 순으로 sort한다.

ex) ["20,111","111"] -> ["111", "20,111"]

3) ,을 기준으로 숫자로 변환하고 모두 vector에 담는다.

4) 중복을 제거한다. (정렬없는 중복제거 인터넷에서 찾아서 그냥 했음)

 

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
template <typename ForwardIterator>
ForwardIterator remove_duplicates( ForwardIterator first, ForwardIterator last )
{
    auto new_last = first;
    for ( auto current = first; current != last; ++current )
    {
        if ( std::find( first, new_last, *current ) == new_last )
        {
            if ( new_last != current ) *new_last = *current;
            ++new_last;
        }
    }
    return new_last;
}
using namespace std;
bool comp_length(string s1, string s2) {
	if (s1.size() == s2.size()) return s1 < s2;
	else return s1.size() < s2.size();
}

vector<int> solution(string s) {
    vector<int> answer;
    vector<string> v;
    int i=1;
    while(i<s.size()){
        
        if(s[i]=='{'){
            vector<char> tmp;
            int j=0;
            i++;
            while(s[i] != '}'){
                tmp.push_back(s[i]);
                i++; j++;
            }

            string c(tmp.begin(),tmp.end());
            v.push_back(c);
        }
        i++;
    }
    sort(v.begin(),v.end(),comp_length);
    answer.push_back(stoi(v[0]));
    
    for(int j=1;j<v.size();j++){
        vector<char> a;
        for(int k=0;k<v[j].size();k++){
            if(v[j][k]==','){
                string kk(a.begin(),a.end());
                answer.push_back(stoi(kk));
                a.clear();
            }
            
            else if(k+1 ==v[j].size()){
                a.push_back(v[j][k]);
                string kk(a.begin(),a.end());
                answer.push_back(stoi(kk));
                a.clear();
            }
            
            else{
                a.push_back(v[j][k]);
            }
            
        }
        cout << endl;
    }
    
    answer.erase(remove_duplicates(answer.begin(),answer.end()),answer.end());
    return answer;
}

 

문자열 쪼개기 연습하고 깔끔해진 풀이

 

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <sstream>
using namespace std;

template <typename ForwardIterator>
ForwardIterator remove_duplicates( ForwardIterator first, ForwardIterator last )
{
    auto new_last = first;
    for ( auto current = first; current != last; ++current )
    {
        if ( std::find( first, new_last, *current ) == new_last )
        {
            if ( new_last != current ) *new_last = *current;
            ++new_last;
        }
    }
    return new_last;
}


bool cmp_length(vector<int>& a, vector<int>& b){
    if(a.size()>=b.size())return false;
    else return true;
}

vector<int> solution(string s) {
    vector<int> answer;
    vector<vector<int>> tupleSet;
    
    // 1) 문자열 쪼개기
    for(int i=1;i<s.size()-1;i++){ // { , }빼고 돌린다.
        if(s[i] == '{'){
            int idx = i+1;
            vector<int> a;
            for(int j=i+1;j<s.size();j++){
                if(s[j]==','){
                    a.push_back(stoi(s.substr(idx,j-idx)));
                    idx = j+1;
                }
                
                if(s[j]=='}'){
                    a.push_back(stoi(s.substr(idx,j-idx)));
                    idx = j+1;
                    tupleSet.push_back(a);
                    break;
                }
            }
        }
    }
    
    /* 디버깅용
    for(int i=0;i<tupleSet.size();i++){
        for(int j=0;j<tupleSet[i].size();j++){
            cout << tupleSet[i][j] << " ";
        }
        cout << endl;
    }
    */
    
    // 2) 길이 순으로 sort
    sort(tupleSet.begin(),tupleSet.end(), cmp_length);
    
    // 3) answer vector에 담기
    for(int i=0;i<tupleSet.size();i++){
        for(int j=0;j<tupleSet[i].size();j++){
            answer.push_back(tupleSet[i][j]);
        }
    }
    
    // 4) 중복제거
    answer.erase(remove_duplicates(answer.begin(),answer.end()),answer.end());
    
    return answer;
}