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 |
29 | 30 |
Tags
- 코테
- vector
- 정석
- 스택
- Stack
- 알고리즘
- 코딩
- DP
- 구현
- 너비우선탐색
- 컴공
- 오퍼레이팅시스템
- 컴퓨터공학과
- 문제풀이
- 개발
- cs
- coding
- bfs
- Computer science
- 컴공과
- 백준
- Operating System
- 정석학술정보관
- 자료구조
- 북리뷰
- 오에스
- c++
- 브루트포스
- 그래프
- OS
Archives
- Today
- Total
Little Jay
[C++] 백준 1406번 - 에디터 본문
스택을 활용하는 전형적인 문제
처음에 커서라는 단어를 봤을 때 이걸 환영 링크드 리스트로 구현을 해야되나라고 생각을 했었는데
생각을 해보니까 커서를 기준으로 왼쪽, 오른쪽으로 스택을 나누어 풀면 쉽겠다라고 생각했다.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
string s;
cin >> s;
int n;
cin >> n;
string temp;
stack<char> left;
stack<char> right;
for (int i = 0; i < s.length(); i++) {
left.push(s[i]);
}
for (int i = 0; i < n; i++) {
cin >> temp;
if (temp == "L") {
if (left.empty())
continue;
else {
right.push(left.top());
left.pop();
}
}
else if (temp == "D") {
if (right.empty())
continue;
else {
left.push(right.top());
right.pop();
}
}
else if (temp == "B") {
if (left.empty())
continue;
else {
left.pop();
}
}
else if (temp == "P") {
char x;
cin >> x;
left.push(x);
}
}
while (!left.empty()) {
right.push(left.top());
left.pop();
}
while (!right.empty()) {
cout << right.top();
right.pop();
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1260번 - DFS와 BFS (0) | 2021.08.19 |
---|---|
[C++] 백준 1931번 - 회의실 배정 (0) | 2021.08.18 |
[C++] 백준 4889번 - 안정적인 문자 (0) | 2021.08.17 |
[C++] 백준 7568번 - 덩치 (0) | 2021.08.17 |
[C++] 백준 1764번 - 듣보잡 (0) | 2021.08.17 |
Comments