일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 인공지능
- supervised learning
- 선형회귀
- 머신러닝 강좌
- JAVA강좌
- 머신러닝 강의
- 딥러닝공부
- 자바강좌
- java
- 효묘블로그
- 지도학습
- unsupervised learning
- 머신러닝
- 자바시작하기
- python강좌
- 머신러닝공부
- C언어
- 파이썬강좌
- Python강의
- 백준 알고리즘
- acmicpc.net
- 비지도학습
- 딥러닝
- feature scaling
- 비용함수
- c언어 오목
- 자바
- 경사하강법
- Gradient Descent
- 파이썬강의
Archives
- Today
- Total
컴공과컴맹효묘의블로그
백준[1406] 에디터 본문
반응형
https://www.acmicpc.net/problem/1406
풀이 방법 첫 번째: linked list로 구현.
두 번째: stack로 구현
세 번째: list로 구현
효율성은 stack > list > linked list 순으로 좋다.
1. stack의 풀이는 두 개의 stack을 만들어서 커서 기준으로 좌측은 s1, 우측은 s2로 저장한다.
2. list는 풀이가 조금 어렵다. dat, pre, nxt라는 배열을 만들고 nxt에 따라 값을 출력한다. dat는 입력된 char들을 순서대로 저장해놓은 배열이다. nxt는 해당 커서에 몇 번째 dat를 출력해야하는지에대한 정보이다. pre는 이전에 출력해야할 char의 index정보이다. dat[0]은 항상 -1이고 dat[1]부터 출력한다. 예를 들어 dat가 -1,a,b,c,d이고 nxt가 1,2,4,-1,3이면 출력은 abdc이다.
3. lined list는 말 그대로 lined list를 구현하면 된다.
1. 스택 풀이
// stack 풀이
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
string s;
vector<char> left, right;
cin >> s;
for (int i = 0; i < s.size(); i++) {
left.push_back(s[i]);
}
cin >> n;
for (int i = 0; i < n; i++) {
char cmd;
cin >> cmd;
switch (cmd) {
case 'L':
if (!left.empty()) {
right.push_back(left.back());
left.pop_back();
}
break;
case 'D':
if (!right.empty()) {
left.push_back(right.back());
right.pop_back();
}
break;
case 'B':
if (!left.empty()) {
left.pop_back();
}
break;
case 'P':
char c;
cin >> c;
left.push_back(c);
break;
}
}
for (int i = 0; i < left.size(); i++) {
cout << left[i];
}
while (!right.empty()) {
cout << right.back();
right.pop_back();
}
return 0;
}
2. list 풀이 출처: https://www.acmicpc.net/source/43759918
백준에서 로그인을 한 뒤에 이 문제를 풀어야 링크가 열린다.
3. linked list 풀이.
#include <iostream>
#include <string>
using namespace std;
typedef struct Char {
char c;
Char* left;
Char* right;
}Char;
// 어떤 문자를 가르키는 것은 그 문자 바로 오른쪽에 커서가 있는 것.
// 맨 앞에 보이지 않는 가상의 문자를 두어야 할것.
// left == null이면 첫 문자. 첫 문자는 c == 0이다.
// right == null이면 커서가 맨 뒤에 있다는 뜻.
int main() {
string s;
Char firstChar;
firstChar.c = NULL;
firstChar.left = NULL;
firstChar.right = NULL;
Char* cursor = &firstChar;
int n;
cin >> s;
for (int i = 0; i < s.size(); i++) {
Char* c = new Char();
c->c = s[i];
c->left = cursor;
c->right = NULL;
cursor->right = c;
cursor = c;
}
cin >> n;
for (int i = 0; i < n; i++) {
char command;
cin >> command;
switch (command) {
case 'L':
if(cursor->c != NULL)
cursor = cursor->left;
break;
case 'D':
if (cursor->right != NULL)
cursor = cursor->right;
break;
case 'B':
if (cursor->left != NULL) {
cursor = cursor->left;
cursor->right = cursor->right->right;
if (cursor->right != NULL)
cursor->right->left = cursor;
}
break;
case 'P':
char t;
cin >> t;
Char* insert = new Char();
insert->c = t;
insert->left = cursor;
insert->right = cursor->right;
if (cursor->right != NULL) {
cursor->right->left = insert;
}
cursor->right = insert;
cursor = insert;
break;
}
}
cursor = firstChar.right;
while (cursor != NULL) {
cout << cursor->c;
cursor = cursor->right;
}
return 0;
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
백준[13913]숨바꼭질 4 (0) | 2022.07.16 |
---|---|
백준[14728] 벼락치기 (1) | 2022.07.16 |
백준 알고리즘[1019] 책 페이지 (0) | 2022.03.18 |
백준 알고리즘[9694] 무엇을 아느냐가 아니라 누구를 아느냐가 문제다 (0) | 2020.06.26 |
백준 알고리즘[2410] 2의 멱수의 합 C++ (0) | 2020.06.22 |
Comments