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

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

 

7785๋ฒˆ: ํšŒ์‚ฌ์— ์žˆ๋Š” ์‚ฌ๋žŒ

์ฒซ์งธ ์ค„์— ๋กœ๊ทธ์— ๊ธฐ๋ก๋œ ์ถœ์ž… ๊ธฐ๋ก์˜ ์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ n ≤ 106) ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” ์ถœ์ž… ๊ธฐ๋ก์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ์ฃผ์–ด์ง€๊ณ  "enter"๋‚˜ "leave"๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. "enter"์ธ ๊ฒฝ์šฐ๋Š”

www.acmicpc.net


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

๐ŸŽจ Go

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

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

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

	var n int
	fmt.Fscanln(reader, &n)

	var workers = map[string]int{}
	for i := 0; i < n; i++ {
		var name, commute string
		fmt.Fscanln(reader, &name, &commute)
		if commute == "enter" {
			workers[name] = 1
		} else if commute == "leave" {
			delete(workers, name)
		}
	}

	var keys = []string{}
	for key, _ := range workers {
		keys = append(keys, key)
	}

	sort.Slice(keys, func(i, j int) bool {
		return keys[i] > keys[j]
	})

	for _, key := range keys {
		fmt.Fprintln(writer, key)
	}
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n = int(sys.stdin.readline())

    workers = {}
    for i in range(n):
        name, commute = sys.stdin.readline().split()
        if commute == "enter":
            workers[name] = 1
        elif commute == "leave":
            del workers[name]

    for i in sorted(list(workers.items()), reverse=True):
        print(i[0])
728x90
๋Œ“๊ธ€