๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/๋ฐฑ์ค€

[๋ฐฑ์ค€] #1764 ๋“ฃ๋ณด์žก (Python)

๋“ฃ๋ณด์žก

๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ๊ณผ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜์™€ ๋ช…๋‹จ์ด ๊ฐ๊ฐ ์ฃผ์–ด์งˆ ๋•Œ, ๋“ฃ๋„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜์™€ ๋ช…๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ

์˜ˆ์ œ

์ž…๋ ฅ 3 4 ohhenrie charlie baesangwook obama baesangwook ohhenrie clinton ์ถœ๋ ฅ 2 baesangwook ohhenrie

์˜ˆ์‹œ

๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์€ 3๋ช… ohhenrie, charlie, baesangwook ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์€ 4๋ช… obama, baesangwook, ohhenrie, clinton

์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ, ๋“ฃ๋„ ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์€ baesangwook, ohhenrie ์œผ๋กœ 2๋ช…์ด ๋‚˜์˜จ๋‹ค.

๋ฌธ์ œ ํ’€์ด ์ „๋žต

listen_list, see_list ์— input๊ฐ’์„ ๋„ฃ๊ณ  name = set(listen_list) & set(see_list) ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฒน์น˜๋Š” ์ž…๋ ฅ๊ฐ’์„ ๊ตฌํ•˜๊ณ  sorted(list(name))๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•˜์—ฌ ์‚ฌ์ „์ˆœ์„œ๋กœ ๋‚˜์˜ค๊ฒŒ ํ•œ๋‹ค.

Results

Version Memory Time
Solution 1 42496KB 3628ms
Solution 2 41624KB 128ms

Solution 1

import sys
input = sys.stdin.readline

listen,see = map(int,input().split())
listen_list = []
see_list = []
for i in range(listen):
    listen_list.append(input().strip())
    
for i in range(see):
    see_list.append(input().strip())
    
name = set(listen_list) & set(see_list)
name = sorted(list(name))

print(len(name))
for i in name:
    print(i)

Solution 2

import sys
input = sys.stdin.readline
listen, see = map(int, input().split())
listen_set = set()
see_set = set()

for i in range(listen):
    listen_set.add(input().strip())

for i in range(see):
    see_set.add(input().strip())

name = listen_set & see_set

print(len(name))
for i in sorted(name):
    print(i)

 

 

www.acmicpc.net/problem/1764

 

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

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

www.acmicpc.net