[BOJ] 5430번 : AC

2024. 8. 7. 20:42Algorithm

1. problem :

https://www.acmicpc.net/problem/5430

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;

void parse(string& input, deque<int>& D) {
	int cur = 0;
	for (int i = 1; i + 1 < input.size(); i++) {
		if (input[i] == ',') {
			D.push_back(cur);
			cur = 0;
		}
		else {
			cur = 10 * cur + (input[i] - '0');
		}
	}
	if (cur != 0) {
		D.push_back(cur);
	}
}
void print_result(deque<int>& d) {
	cout << '[';
	for (int i = 0; i < d.size(); i++)
	{
		cout << d[i];
		if (i + 1 != d.size())
			cout << ',';
	}
	cout << "]\n";
}


int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);


	int T;
	cin >> T;
	while (T--) {
		deque<int> D;
		int rev = 0;
		string op;
		bool isWrong = false;
		int n;
		string input;
		cin >> op;
		cin >> n;
		cin >> input;
		parse(input, D);
		for (char c : op) {
			if (c == 'R') {
				rev = 1 - rev;
			}
			else {
				if (D.empty()) {
					isWrong = true;
					break;
				}
				if (!rev) D.pop_front();
				else D.pop_back();
			}
		}
		if (isWrong) cout << "error\n";
		else {
			if (rev) reverse(D.begin(), D.end());
			print_result(D);
		}
	}
}

외우자. parsing하니깐 C++로 까다롭다. 

<source code 출처: https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x07/solutions/5430.cpp >

'Algorithm' 카테고리의 다른 글

[BOJ] 4949번 : 균형잡힌 세상  (0) 2024.08.08
[BOJ] 6198번 : 옥상 정원 꾸미기  (0) 2024.08.07
[BOJ] 1021번 : 회전하는 큐  (0) 2024.08.07
[BOJ] 10866번 : 덱  (0) 2024.08.07
[BOJ] 2164번 : 카드2  (0) 2024.08.07