티스토리 뷰

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

 

5430번: AC

각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.

www.acmicpc.net

 

생각보다 쉽게 풀려서 다행인 문제였습니다. 단순하게 덱을 어떻게 쓸 줄 안다면 쉽게 푸실 수 있는 문제인 것 같습니다. 여기서 여건은 문자열 파싱을 어떻게 하느냐가 제일 중요했던 것 같습니다. 파싱 함수는 인터넷에 돌아다니는 함수를 긁어서 사용했습니다.

 

#include <iostream>
#include <queue>
#include <string>
#include <sstream>
#include <vector>

using namespace std;
vector<string> split(string str, char delimiter);

int main() {
	ios::sync_with_stdio(false);
	cout.tie(nullptr);

	int N, M, C;
	int input;

	string s, num;
	vector<string> number;

	cin >> C;
	for (int i = 0; i < C; i++) {
		deque<int> t1;
		bool error = false;
		cin >> s;
		cin >> N;
		cin >> num;
		num.erase(num.begin());
		num.erase(num.end()-1);
		
		number = split(num, ',');
		for (string s : number) {
			if (atoi(s.c_str())) {
				t1.push_back(atoi(s.c_str()));
			}
		}
		bool check = true;
		for (char c : s) {
			if (c == 'R') {
				if (check)
					check = false;
				else
					check = true;
			}
			else {
				if (t1.empty()) {
					error = true;
					break;
				}
				if (check)
					t1.pop_front();
				else
					t1.pop_back();
			}
		}
		if(error)
			cout << "error";
		else if (check) {
			cout << "[";
			for (int i = 0; i < t1.size(); i++) {
				if (i == t1.size()-1)
					cout << t1[i];
				else
					cout << t1[i] << ",";
			}
			cout << "]";
		}
		else {
			cout << "[";
			for (int i = t1.size() - 1; i >= 0; i--) {
				if (i == 0)
					cout << t1[i];
				else
					cout << t1[i] << ",";
			}
			cout << "]";
		}
		cout << "\n";
	}
}

vector<string> split(string input, char delimiter) {
	vector<string> answer;
	stringstream ss(input);
	string temp;

	while (getline(ss, temp, delimiter)) {
		answer.push_back(temp);
	}

	return answer;
}

드디어 골드 5 왔네요.. 더 노력해서 플레까지 가보겠습니다. 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함