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

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

 

1764๋ฒˆ: ๋“ฃ๋ณด์žก

์ฒซ์งธ ์ค„์— ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ N, ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„๊ณผ, N+2์งธ ์ค„๋ถ€ํ„ฐ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

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

	var dbj = map[string]int{}
	for i := 0; i < n; i++ {
		var input string
		fmt.Fscanln(reader, &input)
		dbj[input]++
	}

	for i := 0; i < m; i++ {
		var input string
		fmt.Fscanln(reader, &input)
		dbj[input]++
	}

	keys := []string{}
	for key, val := range dbj {
		if val == 2 {
			keys = append(keys, key)
		}
	}

	sort.Strings(keys)

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

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n, m = list(map(int, sys.stdin.readline().split()))
    dbj = {}
    for i in range(n):
        input = sys.stdin.readline().rstrip()
        if dbj.get(input):
            dbj[input] += 1
        else:
            dbj[input] = 1

    for i in range(m):
        input = sys.stdin.readline().rstrip()
        if dbj.get(input):
            dbj[input] += 1
        else:
            dbj[input] = 1

    dbj_result = []
    for (key, value) in dbj.items():
        if value == 2:
            dbj_result.append(key)

    dbj_result.sort()
    print(len(dbj_result))
    for i in dbj_result:
        print(i)
728x90
๋Œ“๊ธ€