알고리즘/자료구조

[백준]1764 듣보잡 #set,dict,list

씩씩한 IT블로그 2020. 6. 16. 23:01
반응형

list는 set과 dictionary 보다 요소를 찾는 속도가 느리다.

1. list(시간초과)

N,M=map(int,input().split())
L=[]
for i in range(N):
    L.append(input())

ans=0
ansL=[]
for i in range(M):
    temp=input()
    if temp in L:
        ans+=1
        ansL.append(temp)
        L.remove(temp)

print(ans)
for i in sorted(ansL):
    print(i)

 

2. set(통과)

N,M=map(int,input().split())
s=set()

for i in range(N):
    word=input()
    s.add(word)

ans=0
ansS=set()
for j in range(M):
    word = input()
    if word in s:
        ans+=1
        ansS.add(word)
        s.remove(word)

print(ans)
for i in sorted(ansS):
    print(i)

 

3. dictionary(통과)

N,M=map(int,input().split())
dict={}
for i in range(N):
    word=input()
    dict[word]=word

ans=0
ansL=[]
for j in range(M):
    word = input()
    if word in dict:
        ans+=1
        ansL.append(word)
        del dict[word]

print(ans)
for i in sorted(ansL):
    print(i)
반응형