๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 5430๋ฒˆ - AC

by ์•ˆ์ฃผํ˜• 2022. 6. 19.

๋ฌธ์ œ

 

5430๋ฒˆ: AC

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ •์ˆ˜ ๋ฐฐ์—ด์— ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ์—๋Š” error๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <deque>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

void parsing(string s, deque<int> &dq){
    istringstream ss(s);
    string stringbuffer;
    while(getline(ss, stringbuffer, ',')){
        dq.push_back(stoi(stringbuffer));
    }
}
int main(){
    int T; cin>>T;
    while(T--){
        string cmd; cin>>cmd;
        int len; string arr; cin>>len>>arr;
        deque<int> dq;
        vector<int>v;
        reverse(arr.begin(), arr.end());
        arr.pop_back();
        reverse(arr.begin(), arr.end());
        arr.pop_back();
        
        parsing(arr, dq);
        bool flag = true, rev = false;
        for(char c: cmd){
            if(c=='D'){
                if(dq.empty()){
                    flag = false;
                    break;
                }
                if(!rev) dq.pop_front();
                else dq.pop_back();
            }
            else if(c=='R'){
                rev=!rev;
            }
        }
        if(!flag) cout<<"error"<<'\n';
        else{
            if(!rev){
                cout<<"[";
                while(!dq.empty()){
                    cout<<dq.front();
                    dq.pop_front();
                    if(dq.size()>=1)cout<<",";
                }
                cout<<"]"<<'\n';;
            }
            else{
                cout<<"[";
                while(!dq.empty()){
                    cout<<dq.back();
                    dq.pop_back();
                    if(dq.size()>=1)cout<<",";
                }
                cout<<"]"<<'\n';;
            }
        }
    }
}

 

ํ’€์ด

#include <iostream>
#include <deque>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

void parsing(string s, deque<int> &dq){
    istringstream ss(s);
    string stringbuffer;
    while(getline(ss, stringbuffer, ',')){
        dq.push_back(stoi(stringbuffer));
    } 
}
int main(){
    int T; cin>>T;
    while(T--){
        string cmd; cin>>cmd;
        int len; string arr; cin>>len>>arr;
        deque<int> dq;
        vector<int>v;
        reverse(arr.begin(), arr.end());
        arr.pop_back();
        reverse(arr.begin(), arr.end());
        arr.pop_back();
        
        parsing(arr, dq);
        bool flag = true;
        for(char c: cmd){
            if(c=='D'){
                if(dq.empty()){
                    flag = false;
                    break;
                }
                dq.pop_front();
            }
            else if(c=='R'){
                reverse(dq.begin(), dq.end());
            }
        }
        if(!flag) cout<<"error"<<'\n';
        else{
            cout<<"[";
            while(!dq.empty()){
                cout<<dq.front();
                dq.pop_front();
                if(dq.size()>=1)cout<<",";
            }
            cout<<"]"<<'\n';;
        }
    }
}

์ฒ˜์Œ ์ œ์ถœํ–ˆ๋˜ ์ฝ”๋“œ.. ๋‹น์—ฐํžˆ for๋ฌธ ๋‚ด์—์„œ reverse ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— O(100,000 * 100,000)๊ฐ€ ๋˜์–ด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด rev๋ผ๋Š” bool ๊ฐ’์„ ํ†ตํ•ด ๋ฐฐ์—ด์ด ๋’ค์ง‘ํžŒ ์ƒํƒœ์ธ์ง€, ์•„๋‹Œ์ง€๋ฅผ ๊ตฌ๋ณ„ํ•ด์„œ rev์˜ ์ƒํƒœ์— ๋”ฐ๋ผ D์—ฐ์‚ฐ์„ deque์˜ front์—์„œ ์ œ๊ฑฐํ•  ๊ฒƒ์ธ์ง€, back์—์„œ ์ œ๊ฑฐํ•  ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ถœ๋ ฅ๋„ rev์˜ ์ƒํƒœ์— ๋”ฐ๋ผ front ํ˜น์€ back์—์„œ ์ถœ๋ ฅํ• ์ง€ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ parsing ํ•˜๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ๊นŒ๋‹ค๋กœ์› ์„ ๋ฟ ํฌ๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š์€ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€