ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

3986๋ฒˆ: ์ข‹์€ ๋‹จ์–ด

์ด๋ฒˆ ๊ณ„์ ˆํ•™๊ธฐ์— ์‹ฌ๋ฆฌํ•™ ๊ฐœ๋ก ์„ ์ˆ˜๊ฐ• ์ค‘์ธ ํ‰์„์ด๋Š” ์˜ค๋Š˜ ์ž์ •๊นŒ์ง€ ๋ณด๊ณ ์„œ๋ฅผ ์ œ์ถœํ•ด์•ผ ํ•œ๋‹ค. ๋ณด๊ณ ์„œ ์ž‘์„ฑ์ด ๋„ˆ๋ฌด ์ง€๋ฃจํ–ˆ๋˜ ํ‰์„์ด๋Š” ๋…ธํŠธ๋ถ์— ์—Ž๋“œ๋ ค์„œ ๊พธ๋ฒ…๊พธ๋ฒ… ์กธ๋‹ค๊ฐ€ ์ œ์ถœ ๋งˆ๊ฐ 1์‹œ๊ฐ„ ์ „์—

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/3986
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)
	var count int
	for i := 0; i < n; i++ {
		var input string
		input, _ = reader.ReadString('\n') // ๊ณต๋ฐฑ ํฌํ•จํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์œ„ํ•ด ReadString() ์‚ฌ์šฉ
		inputs := strings.Split(strings.ReplaceAll(input, "\n", ""), "")
		stack := []string{inputs[0]}
		for j := 1; j < len(inputs); j++ {
			if len(stack) == 0 {
				stack = []string{inputs[j]}
			} else if stack[len(stack)-1] == inputs[j] {
				stack = stack[:len(stack)-1]
			} else {
				stack = append(stack, inputs[j])
			}
		}
		if len(stack) == 0 {
			count++
		}
	}
	fmt.Fprintln(writer, count)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/3986
import sys

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    count = 0
    for i in range(n):
        inputs = list(sys.stdin.readline().rstrip())
        stack = [inputs[0]]
        for j in range(1, len(inputs)):
            if len(stack) == 0:
                stack = [inputs[j]]
            elif stack[-1] == inputs[j]:
                stack.pop()
            else:
                stack.append(inputs[j])
        if len(stack) == 0:
            count += 1
    print(count)
728x90
๋Œ“๊ธ€