๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‘œํŽธ์ง‘ 2021 ์นด์นด์˜ค ์ธํ„ด์‹ญ STL(set)

by Jouureee 2021. 9. 9.

๋ฌธ์ œ : 

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

 

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

8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z"] "OOOOXOOO" 8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"] "OOXOXOOO"

programmers.co.kr

 

๋ถ„์„ : 

์ฃผ์–ด์ง„ ํ‘œ ์ •๋ณด๋ฅผ ๋ฒกํ„ฐ์— ๋‹ด์•„ erase, insert๋“ฑ ๊ธฐ๋ณธ STL ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ์—ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋งž์•˜์œผ๋‚˜ ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ, ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ segmentation fault ์—๋Ÿฌ๋ฅผ ๋ฐ›์•˜๊ณ  ๋ฒกํ„ฐ ๊ณต๊ฐ„์„ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ์‚ฝ์ž…ํ•˜๋Š” ๊ณผ์ •์—์„œ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๋Š” ๊ฒƒ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋ฒ”ํ•œ๊ฑฐ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜๋‹ˆ set์ด ์›์†Œ๋ฅผ ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค ์ž๋™์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด ๋“ค์–ด ๊ฐ„๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค ..! 

๊ทธ๋ž˜์„œ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋‹ด๋Š” ๊ทธ๋ฆ‡์„ set์œผ๋กœ ๋ฐ”๊พธ์—ˆ๊ณ  iterator๋ฅผ ์ด์šฉํ•ด U, D, C, Z ์‹œ ์ธ๋ฑ์Šค๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์›€์ง์—ฌ ์ฃผ์—ˆ๋‹ค. 

 

ํ‹€๋ฆฐ ์ ‘๊ทผ ์ฝ”๋“œ : (vector ์ด์šฉ)

#include <string>
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

vector<int> reV;
stack<pair<int, int>> s;

string solution(int n, int k, vector<string> cmd) {
    string answer = "";
    for(int i = 0; i < n; i++){
        reV.push_back(i);
    }
    
    
    int moving = k;
    vector<int>::iterator nowArray = reV.begin();
    
       for(int i =0; i < cmd.size(); i++){
           nowArray = reV.begin() + moving; // ํ˜„์žฌ ์œ„์น˜ ๊ฐ’ 2
               //c cmd ์ˆ˜ํ–‰
               if(cmd[i] == "C"){// ์‚ญ์ œ
                   s.push({moving, *nowArray});// ์‚ญ์ œ ์ธ๋ฑ์Šค, ์›์†Œ ๊ฐ’
                   reV.erase(nowArray);
                   //reV.resize(n);
                   if(*nowArray == 7){ // ๋งˆ์ง€๋ง‰
                       moving -=1;
                   }
                   continue;
            	//z cmd ์ˆ˜ํ–‰
               }else if(cmd[i] == "Z"){
                  
                   if(!s.empty()){
                       int index = s.top().first;
                       int value = s.top().second;
                       reV.insert(reV.begin() + index , value);
                       if(index < moving){
                           moving += 1;
                       }
                       s.pop();
                   }
                   continue;
                   
               }else{
                   if(cmd[i][0] == 'D' && (48 <= cmd[i][2] &&  cmd[i][2] <= 57)){
                        moving += cmd[i][2] - '0';
                   }else if(cmd[i][0] == 'U' && (48 <= cmd[i][2] &&  cmd[i][2] <= 57)){
                        moving -= cmd[i][2] - '0';
                   }
            }
          
       }
    answer = "OOOOOOOO";
    while(!s.empty()){
        answer.at(s.top().first) = 'X';
        s.pop();
    }


    return answer;
}

 

c++ ์ฝ”๋“œ : (set ์ด์šฉ)

#include <string>
#include <iostream>
#include <vector>
#include <stack>
#include <set>

using namespace std;

//vector<int> reV;
set<int> ss;


string solution(int n, int k, vector<string> cmd) {
    string answer = "";
    stack<int> s;
    for(int i = 0; i < n; i++){
        ss.insert(i);
    }
    
    set<int> :: iterator it,temp;
    it = ss.begin();
    
    //์ดˆ๊ธฐ ์œ„์น˜ ๊ฐ’ ์„ค์ •
    while(k--){
        it++;
    }

   for(int i =0; i < cmd.size(); i++){
       string str = cmd[i];

       //c cmd ์ˆ˜ํ–‰
       if(str == "C"){// ์‚ญ์ œ
           s.push(*it); // ์›์†Œ ๊ฐ’
           temp = it;
           temp++;
           if(temp == ss.end()){ // ๋งˆ์ง€๋ง‰
               temp = it;
               --temp;
           }
           ss.erase(it);
           it = temp;
        //z cmd ์ˆ˜ํ–‰
       }else if(str == "Z"){
          ss.insert(s.top());
           s.pop();
       }else{
           if(str[0] == 'D'){
               string ss = str.substr(2);
                int val = stoi(ss);
               while(val--){
                   ++it;
               }
           }else if(str[0] == 'U'){
               string ss = str.substr(2);
                int val = stoi(ss);
               while(val--){
                   --it;
               }
           }
    }

   }
    
    int idx=0;
    for(it = ss.begin(); it!=ss.end();it++){
        int val = *it;
        if(val==idx){
            answer+='O';
        } 
        else{
            while(idx<val){
                answer+='X';
                idx++;
            }
            answer+='O';
        }
        idx++;
    }
    while(idx<n){		//"OOOXX" ๊ฐ™์€ ์˜ˆ์™ธ ์žก์•„์ฃผ๊ธฐ ์œ„ํ•จ
        answer+='X';
        idx++;
    }

    return answer;
}

 

๋Œ“๊ธ€