728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/9012
ํ์ด์ฌ
# ๋ฐฑ์ค 9012 - ๊ดํธ
T = int(input())
result = []
for i in range(T):
isVPS = input()
left = isVPS.count('(')
right = isVPS.count(')')
if left == right:
result.append("YES")
else:
result.append("NO")
for r in result:
print(r)
๊ทธ๋ฅ count๋ก ๋น๊ตํด์ ()๊ฐ์๊ฐ ๋ค๋ฅด๋ฉด ํ๋ฆฐ๊ฒ ์๋๊ฐ? ์๊ฐํ๋๋ฐ
์์๋ฅผ ๊ณ ๋ คํ์ง ์์๋ค.
์์๋ฅผ ๊ณ ๋ คํ๋ ค๋ฉด Stack ์ ์จ์ผํ๋ค.
์คํ
๋์ค์ ๋ฃ์ ๊ฒ์ด ๋จผ์ ๋์ค๋(LIFO, Last In First Out) ๊ตฌ์กฐ
T = int(input())
for i in range(T):
stack = []
isVPS = input()
for c in isVPS:
if c == '(':
stack.append(c)
elif c == ')':
if len(stack) == 0:
stack.append(c)
break
else:
stack.pop()
if len(stack) !=0:
print("NO")
else:
print("YES")
์๋ฐ์คํฌ๋ฆฝํธ
const input = require("fs")
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
.toString()
.trim()
.split("\n")
const T = Number(input[0]);
for (let i = 1; i <= T; i++) {
const line = input[i];
const stack = [];
for (const c of line){
if (c == '(') {
stack.push(c);
} else if (c == ')') {
if (stack.length == 0){
stack.push(c);
break;
}
else{ stack.pop();}
}
}
if (stack.length != 0){
console.log("NO")
}else{
console.log("YES")
}
}
728x90
๋ฐ์ํ