본문 바로가기

About/Algorithm

[프로그래머스][C++] 튜플

출처 프로그래머스

https://programmers.co.kr/

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#define NOT_FOUND string::npos
 
using namespace std;
 
vector<int> solution(string s) {
    vector<int> answer;
    
    string sub_str;
    int first, last;
    int last_coma;
    int num_coma;
    int check = 0;
    s.erase(s.begin());
    s.erase(s.end() - 1);
    vector<string> str_vector(count(s.begin(), s.end(), '{'), "");
 
 
    while (s.find_first_of('{'!= NOT_FOUND) {
        first = s.find_first_of('{');
        last = s.find_first_of('}');
        sub_str = s.substr(first, last - first + 1);
        num_coma = count(sub_str.begin(), sub_str.end(), ',');
        sub_str.erase(sub_str.begin());
        sub_str.erase(sub_str.end() - 1);
 
        str_vector[num_coma] = sub_str;
        if (last + 1 != s.size() && s[last + 1== ',')
            s.erase(first, last - first + 2);
        else
            s.erase(first, last - first + 1);
    }
    // ex) str_vector[0] = "3" , str_vector[1] = "3, 1" , str_vector[2] = "3, 1, 2"
    for (int i = 0; i < str_vector.size(); i++) {
        last_coma = str_vector[i].find_last_of(',');
        if (last_coma == NOT_FOUND) {
            answer.push_back(stoi(str_vector[i]));
            continue;
        }
        sub_str = str_vector[i].substr(last_coma + 1, str_vector[i].size() - last_coma);
        check = stoi(sub_str);
        if (find(answer.begin(), answer.end(), check) != answer.end()) {
            //already exist number
            str_vector[i].erase(last_coma, s.size() - last_coma);
            i--;
            continue;
        }
        else
            answer.push_back(check);
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 s.erase(s.begin());

 s.erase(s.end() - 1);

우선 입력이 이중괄호이므로 양쪽 괄호를 없애준다.

 

 vector<string> str_vector(count(s.begin(), s.end(), '{'), "");

튜플을 잘라서 저장할 vector 생성

 

while (s.find_first_of('{'!= NOT_FOUND) {

        first = s.find_first_of('{');

        last = s.find_first_of('}');

        sub_str = s.substr(first, last - first + 1);

        num_coma = count(sub_str.begin(), sub_str.end(), ',');

        sub_str.erase(sub_str.begin());

        sub_str.erase(sub_str.end() - 1);

 

        str_vector[num_coma] = sub_str;

        if (last + 1 != s.size() && s[last + 1== ',')

            s.erase(first, last - first + 2);

        else

            s.erase(first, last - first + 1);

    }

문자열에서 '{' 가 없을 때 까지 반복한다.

'{' 가나오는 인덱스와

'}' 가 나오는 인덱스를 저장하고

지운다

 

그리고 

    str_vector[num_coma] = sub_str;

벡터에 저장(index = ','의 갯수

그러면 

ex) str_vector[0] = "3" , str_vector[1] = "3, 1" , str_vector[2] = "3, 1, 2"

이런식으로 저장이 된다.

 

   for (int i = 0; i < str_vector.size(); i++) {

        last_coma = str_vector[i].find_last_of(',');

        if (last_coma == NOT_FOUND) {

            answer.push_back(stoi(str_vector[i]));

            continue;

        }

        sub_str = str_vector[i].substr(last_coma + 1, str_vector[i].size() - last_coma);

        check = stoi(sub_str);

        if (find(answer.begin(), answer.end(), check) != answer.end()) {

            //already exist number

            str_vector[i].erase(last_coma, s.size() - last_coma);

            i--;

            continue;

        }

        else

            answer.push_back(check);

 

그 다음 ','로 구분지으며 숫자를 판단한다.

만약 ','가 없을 경우 answer에 push back 하고

그렇지 않을 경우

,를 빼고 숫자를 자른 후 

answer에 이미 있는 수 이면

string에서 지워주는 것을 반복 하고 해당 string을 다시 검사한다.(i--, continue)

 

answer에 없는 수면 answer에 push back 해준다.