티스토리 뷰

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

 

2800번: 괄호 제거

첫째 줄에 음이 아닌 정수로 이루어진 수식이 주어진다. 이 수식은 괄호가 올바르게 쳐져있다. 숫자, '+', '*', '-', '/', '(', ')'로만 이루어져 있다. 수식의 길이는 최대 200이고, 괄호 쌍은 적어도 1개

www.acmicpc.net

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 input = require('fs').readFileSync('./dev/stdin').toString().trim().split('');

const pair = [];
const stack = new Stack();
for (let i = 0; i < input.length; i++) {
	const c = input[i];
	if (c == '(') {
		stack.push(['(', i]);
	} else if (c == ')') {
		const [_, j] = stack.pop();
		pair.push([j, i]);
	}
}
const answer = [];
for (let i = 1; i < 1 << pair.length; i++) {
	let x = i;
	const temp = [...input];
	const exclude = [];
	let combination = 0;
	while (x > 0) {
		if ((x & 1) == 1) {
			exclude.push(...pair[combination]);
		}
		x >>= 1;
		combination++;
	}
	answer.push(temp.filter((v, i) => !exclude.includes(i)).join(''));
}
console.log([...new Set(answer)].sort().join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함