[๋ฐฑ์ค 9012๋ฒ]๊ดํธ
๋ฌธ์
https://www.acmicpc.net/problem/9012
9012๋ฒ: ๊ดํธ
๊ดํธ ๋ฌธ์์ด(Parenthesis String, PS)์ ๋ ๊ฐ์ ๊ดํธ ๊ธฐํธ์ธ ‘(’ ์ ‘)’ ๋ง์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ ๋ฌธ์์ด์ด๋ค. ๊ทธ ์ค์์ ๊ดํธ์ ๋ชจ์์ด ๋ฐ๋ฅด๊ฒ ๊ตฌ์ฑ๋ ๋ฌธ์์ด์ ์ฌ๋ฐ๋ฅธ ๊ดํธ ๋ฌธ์์ด(Valid PS, VPS)์ด๋ผ๊ณ
www.acmicpc.net
๋ฌธ์ ํ์ด
https://choi95.tistory.com/74?category=881084
[์คํ]์ฌ๋ฐ๋ฅธ ๊ดํธ
๋ฌธ์ ๊ดํธ๊ฐ ์ ๋ ฅ๋๋ฉด ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด๋ฉด "YES", ์ฌ๋ฐ๋ฅด์ง ์์ผ๋ฉด "NO"๋ฅผ ์ถ๋ ฅํฉ๋๋ค.(())() ์ด๊ฒ์ ๊ดํธ์ ์์ด ์ฌ๋ฐ๋ฅด๊ฒ ์์นํ๋ ๊ฑฐ์ง๋ง, (()()))) ์ ์ฌ๋ฐ๋ฅธ ๊ดํธ๊ฐ ์๋๋ค. ๋ฌธ์ ํ์ด ์คํ ์คํ(stack)
choi95.tistory.com
๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ ํ์ด๋ ์ด์ ํฌ์คํ ์์์ ์ฌ๋ฐ๋ฅธ ๊ดํธ ๋ฌธ์ ์ ๊ฐ์ง๋ง input ๊ฐ์ ๋ฐ์ ์ ์ ํ๋ ์์ ์ ์ฐ์ตํ๊ธฐ ์ํด์ ํด๋น ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์๋ค.
์ฝ๋
const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
const cnt = Number(input[0]);
for(let i = 1; i <= cnt; i++) {
let answer = "YES";
let stack = [];
for(let j = 0; j < input[i].length; j++) {
if(input[i][j] === '(') stack.push(input[i][j]);
else {
if(stack.length === 0) {
answer = "NO";
break;
}
stack.pop();
}
}
if(stack.length > 0) answer = "NO";
console.log(answer);
}