ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/4949
// ์ฃผ์ด์ง ๋ฌธ์์ด์ด ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ธ์ง ํ๋จํ๋ ๋ฌธ์ 2
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for {
var input string
input, _ = reader.ReadString('\n') // ๊ณต๋ฐฑ ํฌํจํ์ฌ ์
๋ ฅ ๋ฐ๊ธฐ ์ํด ReadString() ์ฌ์ฉ
convInput := strings.ReplaceAll(input, "\n", "")
if convInput == "." {
break
}
fmt.Fprintln(writer, isBalanced(convInput))
}
}
func isBalanced(input string) (result string) {
var stack []string
for _, v := range input {
strV := string(v)
if strV == "(" || strV == "[" {
stack = append(stack, strV)
} else if strV == ")" {
if len(stack) > 0 && stack[len(stack)-1] == "(" {
stack = stack[:len(stack)-1]
} else {
return "no"
}
} else if strV == "]" {
if len(stack) > 0 && stack[len(stack)-1] == "[" {
stack = stack[:len(stack)-1]
} else {
return "no"
}
} else {
continue
}
}
if len(stack) > 0 {
return "no"
}
return "yes"
}
๐จ Python3
# https://www.acmicpc.net/problem/4949
# ์ฃผ์ด์ง ๋ฌธ์์ด์ด ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ธ์ง ํ๋จํ๋ ๋ฌธ์ 2
import sys
def is_balanced(input):
stack = []
for v in input:
if v == "(" or v == "[":
stack.append(v)
elif v == ")":
if len(stack) > 0 and stack[-1] == "(":
stack = stack[:-1]
else:
return "no"
elif v == "]":
if len(stack) > 0 and stack[-1] == "[":
stack = stack[:-1]
else:
return "no"
else:
continue
if len(stack) > 0:
return "no"
return "yes"
if __name__ == "__main__":
while True:
input = sys.stdin.readline().rstrip('\n')
if input == ".":
break
print(is_balanced(input))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 18258๋ฒ / ํ 2 [Go][Python3] (0) | 2020.11.09 |
---|---|
BOJ / 1874๋ฒ / ์คํ ์์ด [Go][Python3] (0) | 2020.11.08 |
BOJ / 9012๋ฒ / ๊ดํธ [Go][Python3] (0) | 2020.11.06 |
BOJ / 2004๋ฒ / ์กฐํฉ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.05 |
BOJ / 1676๋ฒ / ํฉํ ๋ฆฌ์ผ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.04 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- Golang
- ์ด๋ถํ์
- ์๊ฐ๊ต์ฒด
- ๋งฅ๋ถ
- ๋ธ๋ฃจํธํฌ์ค
- ๋ชฝ๊ณ ๋๋น
- dp
- AWS
- Algorithm
- ๋ฐฑ์ค
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- MongoDB
- python3
- ํด์๋งต
- ํ๋ก์ด๋์์ฌ
- ์คํ
- baekjoon
- ballet
- java
- BOJ
- ๋ฐ๋
- ์๋ฐ
- Macbook pro 2012 mid 13
- ํ
- BFS
- ๋ถํ ์ ๋ณต
- dfs
- ๋งฅ๋ถํ๋ก
- go
- ์๊ณ ๋ฆฌ์ฆ
- Total
- Today
- Yesterday