문제
해결 과정
시행착오
- 술술 풀리면 항상 시간초과인 것 같다.. 너무 정직하게 풀었다.
import sys
n,m = map(int,sys.stdin.readline().split())
never_heard = []
never_seen = []
for _ in range(n):
never_heard.append(sys.stdin.readline().strip())
for _ in range(m):
never_seen.append(sys.stdin.readline().strip())
result = []
for i in range(n):
for j in range(m):
if never_heard[i] == never_seen[j]:
result.append(never_heard[i])
result.sort()
print(len(result))
for n in result:
print(n)
풀이
import sys
n,m = map(int,sys.stdin.readline().split())
never_heard = set()
never_seen = set()
for _ in range(n):
never_heard.add(sys.stdin.readline().strip())
for _ in range(m):
never_seen.add(sys.stdin.readline().strip())
result = never_heard.intersection(never_seen)
result = sorted(result)
print(len(result))
for n in result:
print(n)