ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/10799
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var input string
fmt.Fscanln(reader, &input)
inputs := strings.Split(input, "")
fmt.Fprintln(writer, getBarCount(inputs))
}
func getBarCount(inputs []string) int {
var stack []string
var prev string
var barCount int
for i := 0; i < len(inputs); i++ {
cur := inputs[i]
if cur == "(" {
stack = append(stack, cur)
} else if cur == ")" {
stack = stack[0 : len(stack)-1]
if prev == "(" { // "(" ๋ค์ ")"๊ฐ ๋ฐ๋ก ๋ค์ด์ค๋ฉด ๋ ์ด์
barCount += len(stack) // ๋ ์ด์ ์ธ ๊ฒฝ์ฐ ์คํ ์์ ๊ดํธ ๊ฐฏ์๋งํผ ๋ง๋ ์นด์ดํธ.
} else {
barCount++ // ")" ๋ค์ ")"๊ฐ ๋ค์ด์จ ๊ฒฝ์ฐ ๋ง๋๊ธฐ ๋์ ๊ฐ์ ํ๋ ์นด์ดํธ.
}
}
prev = cur
}
return barCount
}
๐จ Python3
# https://www.acmicpc.net/problem/10799
import sys
def get_bar_count(inputs):
stack = []
prev = ""
bar_count = 0
for i in range(len(inputs)):
cur = inputs[i]
if cur == "(":
stack.append(cur)
elif cur == ")":
stack.pop()
if prev == "(":
bar_count += len(stack)
else:
bar_count += 1
prev = cur
return bar_count
if __name__ == "__main__":
inputs = list(sys.stdin.readline().rstrip())
print(get_bar_count(inputs))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 4659๋ฒ / ๋น๋ฐ๋ฒํธ ๋ฐ์ํ๊ธฐ [Go][Python3] (0) | 2021.02.08 |
---|---|
BOJ / 11656๋ฒ / ์ ๋ฏธ์ฌ ๋ฐฐ์ด [Go][Python3] (0) | 2021.02.07 |
BOJ / 17219๋ฒ / ๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ [Go][Python3] (0) | 2021.02.05 |
BOJ / 1302๋ฒ / ๋ฒ ์คํธ์ ๋ฌ [Go][Python3] (0) | 2021.02.04 |
BOJ / 3986๋ฒ / ์ข์ ๋จ์ด [Go][Python3] (0) | 2021.02.03 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ํด์๋งต
- ์๊ณ ๋ฆฌ์ฆ
- java
- ๋งฅ๋ถ
- ballet
- dp
- Algorithm
- ๋งฅ๋ถํ๋ก
- ํ๋ก์ด๋์์ฌ
- dfs
- BFS
- python3
- MongoDB
- ์๊ฐ๊ต์ฒด
- ์ด๋ถํ์
- ๋ชฝ๊ณ ๋๋น
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ถํ ์ ๋ณต
- ํ
- ๋ฐ๋
- BOJ
- ์๋ฐ
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐฑ์ค
- AWS
- Golang
- baekjoon
- Macbook pro 2012 mid 13
- ์คํ
- go
- Total
- Today
- Yesterday