Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백트래킹
- 큐
- BeautifulSoup
- 이분 탐색
- 클래스
- BFS
- 완전 탐색
- 우선순위 큐
- 비트마스크
- dfs
- 빠른 입출력
- 피보나치 수
- lis
- 문자열
- Knapsack
- 재귀
- DP
- 분할 정복
- 순열
- MST
- 조합
- SSAFY
- 크루스칼
- 링크드리스트
- 중복 순열
- 세그먼트 트리
- 스택
- 그리디
- 시뮬레이션
- 메모리풀
Archives
- Today
- Total
작심 24/7
[백준] 5430번 AC 본문
처음에 algorithm 라이브러리에 있는 reverse 함수를 썼었는데 시간 초과 걸려서
start 인덱스와 end 인덱스를 가지고 R과 D를 수행했다.
R인 경우에는 start와 end를 교환해주고
D인 경우에는 배열이 비어있을 경우 error처리해주고
그 외엔 start가 그다음 숫자를 가리키게 하였다.
여기서 주의할 점은 입력으로
D
0
[]
가 들어오면
배열이 비어있는데 D를 사용하기 때문에 error를 출력해야 하지만
R
0
[]
이런 식으로 들어오면 R은 error가 날 이유가 없기 때문에 []를 출력해야 한다.
이 부분들을 따로 처리해주면 끝
#include <iostream>
#include <string>
using namespace std;
int main() {
int T;
cin >> T;
string p, arr;
int N;
for (int t = 0; t < T; t++) {
int res[100001] = { 0 }, error = 0;
cin >> p >> N >> arr;
if (N == 0) {
for (int i = 0; i < p.size(); i++) {
if (p[i] == 'D') {
cout << "error\n";
break;
}
else if (i == p.size() - 1) cout << "[]\n";
}
continue;
}
int idx = 0;
string num;
for (int i = 1; i < arr.size(); i++) {
if (arr[i] >= '0' && arr[i] <= '9') num += arr[i];
else if (arr[i] == ',' || arr[i] == ']') {
res[idx] = stoi(num);
idx++;
num = "";
}
}
error = 0;
int start = 0, end = N, temp;
for (int i = 0; i < p.size(); i++) {
if (p[i] == 'R') {
temp = start;
start = end;
end = temp;
}
else {
if (start == end) {
error = 1;
break;
}
if (start > end) start--;
else start++;
}
}
if (error == 1) cout << "error\n";
else {
cout << "[";
if (start < end) {
for (int i = start; i < end; i++) {
if (i == end - 1) cout << res[i];
else cout << res[i] << ",";
}
}
else {
for (int i = start - 1; i >= end; i--) {
if (i == end) cout << res[i];
else cout << res[i] << ",";
}
}
cout << "]\n";
}
}
return 0;
}
'백준' 카테고리의 다른 글
[백준] 1725번 히스토그램 (0) | 2020.06.05 |
---|---|
[백준] 1918번 후위 표기식 (0) | 2020.06.04 |
[백준] 3078번 좋은 친구 (0) | 2020.06.03 |
[백준] 1966번 프린터 큐 (0) | 2020.06.03 |
[백준] 2156번 포도주 시식 (0) | 2020.05.30 |
Comments