๋ฌธ์
https://www.acmicpc.net/problem/1764
1764๋ฒ: ๋ฃ๋ณด์ก
์ฒซ์งธ ์ค์ ๋ฃ๋ ๋ชปํ ์ฌ๋์ ์ N, ๋ณด๋ ๋ชปํ ์ฌ๋์ ์ M์ด ์ฃผ์ด์ง๋ค. ์ด์ด์ ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์ ๊ฑธ์ณ ๋ฃ๋ ๋ชปํ ์ฌ๋์ ์ด๋ฆ๊ณผ, N+2์งธ ์ค๋ถํฐ ๋ณด๋ ๋ชปํ ์ฌ๋์ ์ด๋ฆ์ด ์์๋๋ก ์ฃผ์ด์ง๋ค.
www.acmicpc.net
ํ์ด
# ๋ฐฑ์ค 1764๋ฒ ๋ฌธ์ - ๋ฃ๋ณด์ก
import sys
n, m = map(int, sys.stdin.readline().split())
people_n, people_m = set(), set()
for _ in range(n):
name = sys.stdin.readline().strip()
people_n.add(name)
for _ in range(m):
name = sys.stdin.readline().strip()
people_m.add(name)
answer = sorted(list(people_n & people_m))
print(len(answer))
for i in range(len(answer)):
print(answer[i])
๋ฌธ์ ๋ฅผ ์ ๋๋ก ์ฝ์ง ์๊ณ ํ๋ค๊ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋์์ ๋ค์ ํ์ธํด๋ดค๋๋
์ค๋ณต๋๋ ์ด๋ฆ์ด ์๋ค๋ ๊ฒ์ ์๊ณ set์ ์ฌ์ฉํ์ฌ ์๊ฐ์ ์ค์๋ค.
๋ set์ ์์๊ฐ ์๊ด์์ด ์์ผ๋ฏ๋ก sort๋ฅผ ํตํด ์์๋ฅผ ์ ๋ ฌํด ์ฃผ์๋ค.
๋ ๊น๋ํ ์ฝ๋
# ๋ฐฑ์ค 1764๋ฒ ๋ฌธ์ - ๋ฃ๋ณด์ก
import sys
n, m = map(int, sys.stdin.readline().split())
people_n, people_m = set(), set()
for _ in range(n):
people_m.add( sys.stdin.readline().strip())
for _ in range(m):
people_m.add(sys.stdin.readline().strip())
answer = sorted(list(people_n & people_m))
print(len(answer))
for i in range(len(answer)):
print(answer[i])
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] Class3_2579๋ฒ_๊ณ๋จ ์ค๋ฅด๊ธฐ (0) | 2022.10.28 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] Class3_17129๋ฒ_๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ (0) | 2022.10.28 |
๐ฉ [๋ฐฑ์ค] [Python] Class3_11723๋ฒ_์งํฉ (0) | 2022.10.28 |
[๋ฐฑ์ค] [Python] Class3_1676๋ฒ_ํฉํ ๋ฆฌ์ผ 0์ ๊ฐ์ (0) | 2022.10.28 |
[๋ฐฑ์ค] [Python] Class2_15829๋ฒ_Hashing (0) | 2022.10.27 |