๋ฌธ์
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 ํ๋ ๋ถ๋ถ์ด ์กฐ๊ธ ๊น๋ค๋ก์ ์ ๋ฟ ํฌ๊ฒ ์ด๋ ต์ง๋ ์์ ๋ฌธ์ ์์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 17255๋ฒ - N์ผ๋ก ๋ง๋ค๊ธฐ (0) | 2022.06.20 |
---|---|
[๋ฐฑ์ค,c++] 19537๋ฒ - ์ธ์ด๋ฒ๊ฐ๊ฐ์ดํ (0) | 2022.06.20 |
[๋ฐฑ์ค,c++] 10546๋ฒ - ๋ฐฐ๋ถ๋ฅธ ๋ง๋ผํ ๋ (0) | 2022.06.19 |
[๋ฐฑ์ค,c++] 18115๋ฒ - ์นด๋ ๋๊ธฐ (0) | 2022.06.19 |
[๋ฐฑ์ค,c++] 5397๋ฒ - ํค๋ก๊ฑฐ (0) | 2022.06.03 |
[๋ฐฑ์ค,c++] 1918๋ฒ - ํ์ ํ๊ธฐ์ (0) | 2022.06.03 |
๋๊ธ