티스토리 뷰

https://www.acmicpc.net/problem/5397

 

5397번: 키로거

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한줄로 이루어져 있고, 강산이가 입력한 순서대로 길이가 L인 문자열이 주어진다. (1 ≤ L ≤ 1,000,000) 강산이가 백스페이스를 입

www.acmicpc.net

 

const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
input.shift();
class Node {
	constructor(item) {
		this.item = item;
		this.next = null;
	}
}

class Stack {
	constructor() {
		this.topOfStack = null;
		this.length = 0;
	}

	push(item) {
		const node = new Node(item);
		if (this.topOfStack != null) {
			node.next = this.topOfStack;
		}
		this.topOfStack = node;
		this.length += 1;
	}

	pop() {
		if (this.length == 0) return '';
		const popItem = this.topOfStack;
		this.topOfStack = popItem.next;
		this.length -= 1;

		return popItem.item;
	}

	size() {
		return this.length;
	}

	empty() {
		if (this.length == 0) return true;
		else return false;
	}

	top() {
		if (this.length == 0) return -1;
		return this.topOfStack.item;
	}
}

class Password {
	constructor() {
		this.front = new Stack();
		this.rear = new Stack();
	}

	add(char) {
		this.front.push(char);
	}

	delete() {
		this.front.pop();
	}

	left() {
		if (this.front.size() > 0) this.rear.push(this.front.pop());
	}

	right() {
		if (this.rear.size() > 0) this.front.push(this.rear.pop());
	}

	generate(string) {
		for (let i = 0; i < string.length; i++) {
			const char = string[i];
			switch (char) {
				case '<':
					this.left();
					break;
				case '>':
					this.right();
					break;
				case '-':
					this.delete();
					break;
				default:
					this.add(char);
			}
		}

		let password = '';
		while (!this.front.empty()) {
			password = this.front.pop() + password;
		}
		while (!this.rear.empty()) {
			password = password + this.rear.pop();
		}

		return password;
	}
}

const answer = [];

input.forEach((v) => {
	const keylogger = new Password();
	const pw = keylogger.generate(v);
	answer.push(pw);
});

console.log(answer.join('\n'));
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
글 보관함