티스토리 뷰
https://www.acmicpc.net/problem/1406
1406번: 에디터
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수
www.acmicpc.net
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v=>v.trim());
const answer = [];
let main = input.shift();
const N = +input.shift()
class Node {
constructor(item) {
this.item = item;
this.front = null;
this.back = null;
}
}
//이중연결리스트
class LinkedList {
constructor() {
this.head = new Node('HEAD');
this.cursor = null;
}
push(item){
const node = new Node(item)
if(this.head.back==null){// 비어있을 때.
node.front = this.head;
this.head.back = node;
this.cursor = node;
}else if(this.cursor.back==null){ // 커서가 맨 뒤에 있을 때.
node.front = this.cursor;
this.cursor.back = node;
this.cursor = node;
}else{ // 커서가 중간에 있을 떄.
node.front = this.cursor;
node.back = this.cursor.back;
this.cursor.back.front = node;
this.cursor.back = node;
this.cursor = node;
}
}
pop(){
if(this.cursor.front!=null){ // 커서가 맨앞에 있지 않으면
if(this.cursor.back!=null){ // 커서 뒤에 값이 있으면 조정해줌.
this.cursor.back.front = this.cursor.front;
}
this.cursor.front.back = this.cursor.back;
this.cursor= this.cursor.front;
}
}
forward(){
if(this.cursor.front!=null){
this.cursor = this.cursor.front;
}
}
backward(){
if(this.cursor.back!=null){
this.cursor = this.cursor.back;
}
}
show(){
let printer = this.head.back
const paper = [];
while(printer!=null){
paper.push(printer.item);
printer = printer.back;
}
console.log(paper.join(''))
}
}
let l = new LinkedList();
for(let i = 0; i < main.length; i++){
l.push(main[i])
}
for(let i = 0; i<N; i++){
const command = input[i];
switch(command){
case 'L':
l.forward();
break;
case 'D':
l.backward()
break;
case 'B':
l.pop();
break;
default:
const [_,value] = command.split(' ');
l.push(value);
break;
}
}
l.show()
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js)백준 1181번 : 단어 정렬 (0) | 2021.12.21 |
---|---|
Node.js)백준 1026번 : 보물 (0) | 2021.12.21 |
Node.js) 백준 10845번 : 큐 (0) | 2021.12.20 |
Node.js) 백준 1929번 : 소수 구하기 (0) | 2021.12.20 |
Node.js) 백준 2740번: 행렬 곱셈 (0) | 2021.12.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- node.js
- 서버개발
- 개발자면접
- 로드나인
- 면접비
- 서버점검
- DB 생성
- MOD
- 은둔청년체험
- create databases;
- 투포인터
- BFS
- 면접질문
- 동적프로그래밍
- 롱베케이션
- MySQL
- 최소공통조상
- 투포인터 연습
- 다이나믹프로그래밍
- 그래프
- 다이나밍프로그래밍
- create db
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함