티스토리 뷰
https://www.acmicpc.net/problem/1662
1662번: 압축
압축되지 않은 문자열 S가 주어졌을 때, 이 문자열중 어떤 부분 문자열은 K(Q)와 같이 압축 할 수 있다. K는 한자리 정수이고, Q는 0자리 이상의 문자열이다. 이 Q라는 문자열이 K번 반복된다는 뜻이
www.acmicpc.net
const input = require('fs').readFileSync('./dev/stdin').toString().trim();
class Node {
constructor(item) {
this.item = item;
this.prev = null;
}
}
class Stack {
constructor() {
this.top = null;
this.size = 0;
}
push(item) {
const node = new Node(item);
node.prev = this.top;
this.top = node;
this.size += 1;
}
pop() {
const popItem = this.top;
this.top = this.top.prev;
this.size -= 1;
return popItem.item;
}
}
const stack = new Stack();
for (let i = 0; i < input.length; i++) {
if (stack.length == 0) {
stack.push(input[i]);
} else {
const c = input[i];
switch (c) {
case ')':
let x = 0;
while (stack.top.item != '(') {
const item = stack.pop();
if (typeof item === 'string') {
x += 1;
} else {
x += item;
}
}
stack.pop();
const n = +stack.pop();
stack.push(x * n);
break;
default:
stack.push(c);
break;
}
}
}
let answer = 0;
while (stack.size > 0) {
const item = stack.pop();
if (typeof item === 'string') {
answer += 1;
} else {
answer += item;
}
}
console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1374번: 강의실 (0) | 2023.08.31 |
---|---|
Node.js)백준 19598번: 최소 회의실 개수 (0) | 2023.08.31 |
Node.js) 백준 1351번: 무한 수 (0) | 2023.08.31 |
Node.js) 백준 11000번: 강의실 배정 (0) | 2023.08.31 |
Node.js) 백준 1717번: 집합의 표현 (0) | 2023.08.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- node.js
- 그래프
- create databases;
- 면접질문
- 최소공통조상
- 로드나인
- 개발자면접
- 서버점검
- create db
- MySQL
- 서버개발
- 투포인터 연습
- 면접비
- 은둔청년체험
- BFS
- 동적프로그래밍
- 투포인터
- 다이나밍프로그래밍
- DB 생성
- MOD
- 다이나믹프로그래밍
- 롱베케이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 |
30 | 31 |
글 보관함