Skip to content

Instantly share code, notes, and snippets.

@rockpell
Last active January 21, 2021 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockpell/205ec3b13bb7218c742b9d5b7ea2b2cc to your computer and use it in GitHub Desktop.
Save rockpell/205ec3b13bb7218c742b9d5b7ea2b2cc to your computer and use it in GitHub Desktop.
문제 풀이(백준에서는 입출력 관련 오류가 있음)
// https://www.acmicpc.net/problem/5397
function stdin_text() {
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const input = [];
let isFirstLine = false;
let testcases = 0;
let i = 0;
rl.on('line', function(line){
if (!isFirstLine) {
testcases = parseInt(line);
isFirstLine = true;
}
else {
console.log(keyLogger(line));
i++;
if (i === testcases) {
rl.close();
}
}
});
rl.on('close', function(){
process.exit();
});
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.previos = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
add(node) {
if (this.head === null) {
this.head = node;
this.tail = node;
}
else {
node.previos = this.tail;
this.tail.next = node;
this.tail = node;
}
return this.tail;
}
getHead() {
return this.head;
}
addNext(target, node) {
if (target !== null) {
const nextNode = target.next;
target.next = node;
node.previos = target;
node.next = nextNode;
if (nextNode !== null)
nextNode.previos = node;
return node;
}
else {
this.head = node;
return this.head;
}
}
remove(target) {
if (target != null) {
if (target.previos === null) {
if (target.next === null) {
this.head = null;
return null;
}
else {
this.head = target.next;
return this.head
}
}
else {
const previosNode = target.previos;
const nextNode = target.next;
if (previosNode == null) {
this.head = nextNode;
return this.head;
}
else {
previosNode.next = nextNode;
if (nextNode != null)
nextNode.previos = previosNode;
return this.previosNode
}
}
}
return target;
}
toString() {
let tempNode = this.head;
const strArray = [];
while (tempNode) {
strArray.push(tempNode.data);
tempNode = tempNode.next;
}
return strArray.join('');
}
print() {
let tempNode = this.head;
console.log("--------------");
while (tempNode) {
console.log(tempNode.data);
tempNode = tempNode.next;
}
}
}
function keyLogger(line) {
const passowrdList = new LinkedList();
const passowrd = [];
let cursor = passowrdList.getHead();
for (let ch of line) {
if (ch === '<') {
if (cursor != null && cursor.previos != null) {
cursor = cursor.previos;
}
}
else if (ch === '>') {
if (cursor != null && cursor.next != null) {
cursor = cursor.next;
}
}
else if (ch === '-') {
cursor = passowrdList.remove(cursor);
}
else {
cursor = passowrdList.addNext(cursor, new Node(ch));
}
}
return passowrdList.toString();
}
stdin_text();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment