티스토리 뷰

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