티스토리 뷰
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
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2504번: 괄호의 값 (0) | 2023.06.19 |
---|---|
Node.js) 백준 10799번: 쇠막대기 (0) | 2023.06.18 |
Node.js) 백준 1475번: 방 번호 (0) | 2023.06.17 |
Node.js) 백준 13300번: 방 배정 (0) | 2023.06.17 |
Node.js) 백준 1919번: 애너그램 만들기 (0) | 2023.06.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- create databases;
- MOD
- 은둔청년체험
- 투포인터 연습
- 동적프로그래밍
- 면접질문
- 면접비
- 서버개발
- 다이나믹프로그래밍
- MySQL
- node.js
- 개발자면접
- 그래프
- 로드나인
- create db
- 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 |
글 보관함