회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
이중연결리스트 활용한 풀이, 반례를 모르겠습니다.
1406번 - 에디터
jiheon788
1년 전
0
이중연결리스트를 활용해 테케는 모두 통과하나 반례를 찾지 못하겠습니다. 제가 어느 부분을 놓치고 있는 걸까요?
const fs = require("fs"); const path = require("path"); const filePath = process.platform === "linux" ? "/dev/stdin" : path.join(__dirname, "/example.txt"); const input = fs.readFileSync(filePath).toString().trim().split("\n"); const [initial, n, ...commands] = input; // console.log(initial, n, commands); // Node 클래스 정의 class Node { constructor(value) { this.value = value; // 노드 값 this.next = null; // 다음 노드 포인터 this.prev = null; // 이전 노드 포인터 } } // DoublyLinkedList 클래스 정의 class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (this.head === null) { // 비어있을 때 초기화 -> 머리, 꼬리는 새로운 노드 this.head = newNode; this.tail = newNode; } else { // 꼬리에 이중연결 -> 꼬리의 다음에 새로운 노드 연결, 새로운 노드의 이전은 기존의 꼬리 this.tail.next = newNode; newNode.prev = this.tail; // 꼬리 업데이트 this.tail = newNode; } return newNode; // 마지막 노드를 반환 } // 새로운값은 '애프터노드'의 이전애 삽입 insert(afterNode, value) { const newNode = new Node(value); if (!afterNode) { // 리스트가 비어있거나 처음에 삽입 newNode.next = this.head; if (this.head) this.head.prev = newNode; this.head = newNode; if (!this.tail) this.tail = newNode; } else { newNode.next = afterNode.next; newNode.prev = afterNode; if (afterNode.next) afterNode.next.prev = newNode; afterNode.next = newNode; if (afterNode === this.tail) this.tail = newNode; } return newNode; // 삽입된 노드를 반환 } remove(node) { if (!node) return; if (node === this.head) { this.head = node.next; if (this.head) this.head.prev = null; } else if (node === this.tail) { this.tail = node.prev; if (this.tail) this.tail.next = null; } else { node.prev.next = node.next; node.next.prev = node.prev; } } print() { let current = this.head; const result = []; while (current) { result.push(current.value); current = current.next; } return result.join(""); } } function solution(initial, n, commands) { const linkedList = new DoublyLinkedList(); let cursor = null; // 초기 문자열을 리스트에 추가 for (const char of initial) { cursor = linkedList.append(char); // 초기 문자열의 마지막 위치를 커서로 설정 } // console.log(cursor); // console.log(linkedList.print()); // 명령어 처리 for (let i = 0; i < n; i++) { const [c, _, t] = commands[i].split(""); switch (c) { case "P": // 커서 다음에 새로운 문자를 삽입 cursor = linkedList.insert(cursor, t); break; case "L": // 커서를 왼쪽으로 이동 if (cursor) cursor = cursor.prev; break; case "D": // 커서를 오른쪽으로 이동 if (cursor && cursor.next) cursor = cursor.next; break; case "B": // 커서 왼쪽의 문자를 삭제 if (cursor) { const prev = cursor.prev; // 현재 커서의 이전 노드를 저장 linkedList.remove(cursor); // 현재 커서를 삭제 cursor = prev; // 커서를 이전 노드로 이동 } break; } // console.log( // `${String(i).padStart(2, 0)} | ${c} ${t ?? " "}: ${linkedList.print()}` // ); } return linkedList.print(); } const answer = solution(initial, n, commands); console.log(answer);
댓글을 작성하려면
로그인
해야 합니다.
jiheon788 1년 전
이중연결리스트를 활용해 테케는 모두 통과하나 반례를 찾지 못하겠습니다. 제가 어느 부분을 놓치고 있는 걸까요?