ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/9012
// ์ฃผ์ด์ง ๋ฌธ์์ด์ด ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ธ์ง ํ๋จํ๋ ๋ฌธ์
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
fmt.Fscanln(reader, &t)
for i := 0; i < t; i++ {
var inputStr string
fmt.Fscanln(reader, &inputStr)
fmt.Fprintln(writer, isVPS(inputStr))
}
}
func isVPS(inputStr string) string {
var open, close int
for _, v := range inputStr {
if string(v) == "(" {
open++
} else if string(v) == ")" {
close++
}
if open < close { // ๋ซํ ๊ดํธ ๊ฐ์๊ฐ ๋ ๋ง์์ง๋ฉด "NO" ๋ฆฌํด
return "NO"
}
}
if open != close { // ์ต์ข
๊ดํธ ๊ฐ์๊ฐ ๋ค๋ฅด๋ฉด "NO" ๋ฆฌํด
return "NO"
}
return "YES"
}
// // ์คํ์ผ๋ก ํ๊ธฐ
// func isVPS2(inputStr string) string {
// var stack []string
// for _, v := range inputStr {
// strValue := string(v)
// if strValue == "(" {
// stack = append(stack, strValue)
// } else if strValue == ")" {
// if len(stack) > 0 {
// stack = stack[:len(stack)-1]
// } else {
// return "NO"
// }
// }
// }
// if len(stack) > 0 {
// return "NO"
// }
// return "YES"
// }
๐จ Python3
# https://www.acmicpc.net/problem/9012
# ์ฃผ์ด์ง ๋ฌธ์์ด์ด ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ธ์ง ํ๋จํ๋ ๋ฌธ์
import sys
def is_vps(input_str):
open_count, close_count = 0, 0
for s in input_str:
if s == "(":
open_count += 1
elif s == ")":
close_count += 1
if open_count < close_count:
return "NO"
if open_count != close_count:
return "NO"
return "YES"
if __name__ == "__main__":
t = int(sys.stdin.readline())
for i in range(t):
print(is_vps(sys.stdin.readline()))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1874๋ฒ / ์คํ ์์ด [Go][Python3] (0) | 2020.11.08 |
---|---|
BOJ / 4949๋ฒ / ๊ท ํ์กํ ์ธ์ [Go][Python3] (0) | 2020.11.07 |
BOJ / 2004๋ฒ / ์กฐํฉ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.05 |
BOJ / 1676๋ฒ / ํฉํ ๋ฆฌ์ผ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.04 |
BOJ / 1932๋ฒ / ์ ์ ์ผ๊ฐํ [Python3] (0) | 2020.11.03 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ํ
- dfs
- ๋ธ๋ฃจํธํฌ์ค
- ํด์๋งต
- python3
- go
- ๋ชฝ๊ณ ๋๋น
- BOJ
- dp
- java
- ์๋ฐ
- Macbook pro 2012 mid 13
- ์๊ฐ๊ต์ฒด
- ๋งฅ๋ถ
- AWS
- ๋ฐฑ์ค
- ํ๋ก์ด๋์์ฌ
- ์๊ณ ๋ฆฌ์ฆ
- MongoDB
- ๋ถํ ์ ๋ณต
- ๋ฐ๋
- ๋งฅ๋ถํ๋ก
- ballet
- BFS
- ์คํ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- Golang
- ์ด๋ถํ์
- baekjoon
- Algorithm
- Total
- Today
- Yesterday