잡동사니 블로그
[백준] 5349 - Duplicate SSN (python) 본문
https://www.acmicpc.net/problem/5349
5349번: Duplicate SSN
The U.S. Social Security Administration has made a terrible mistake. When assigning new Social Security numbers (SSN) to U.S. citizens they accidently assigned some duplicate numbers. Fortunately, they caught this mistake early and have a list all the poss
www.acmicpc.net
문제
The U.S. Social Security Administration has made a terrible mistake. When assigning new Social Security numbers (SSN) to U.S. citizens they accidently assigned some duplicate numbers. Fortunately, they caught this mistake early and have a list all the possible duplicates. The Social Security Administration must contact all the people that do not have a unique SSN. Your job is to read the list of Social Security numbers and find all the duplicates. You will then print the list of duplicates in ascending order.
입력
The input is a list of Social Security numbers, one per line. Input will be terminated with the Social Security number 000-00-0000, which is not a duplicate.
출력
A list of all Social Security numbers, in ascending order, that appear more than once as input.
예제 입력 1
555-44-6666
111-99-4444
012-00-1111
888-98-9086
555-44-6666
234-54-3425
012-00-1111
555-44-6666
000-00-0000
예제 출력 1
012-00-1111
555-44-6666
풀이
쉽게말해서 전화번호부에 중복된 전화번호부를 문자열 기준 오름차순으로 정렬해서 출력하면 되는 문제
import sys
input_data = sys.stdin.read().splitlines()
a=set()
c=set()
for b in input_data:
if b == '000-00-0000':
break
if b in a :
c.add(b)
else :
a.add(b)
c=sorted(c)
for i in c:
print(i)
sys.stdin.read().splitlines()로 개행문자를 제거한 채 전부 리스트에 담고, set안에 어떠한 요소를 비교하기 위한 시간복잡도는 O(1)이기 때문에 전부 Set에 집어넣어 비교를 하여 출력할 set을 list로 바꾸어 오름차순 정렬 후, 출력함.
느낀점
import sys
input=sys.stdin.readline
a=set()
c=set()
while True :
b=input()
if b == '000-00-0000':
break
자꾸 시간초과 되길래 왜인가 싶었는데 개행문자 제거를 안했기 때문에 반복문이 멈출 수가 없었음.
import sys
input=sys.stdin.readline
a=set()
c=set()
while True :
b=input().rstrip()
if b == '000-00-0000':
break
이렇게 했어야 하는데
'Python > 백준' 카테고리의 다른 글
[백준] 32403 - 전구 주기 맞추기 (python) (5) | 2024.10.10 |
---|---|
[백준] 1422 - 숫자의 신 (python) (0) | 2023.07.18 |
[백준] 1021 - 회전하는 큐 (python) (0) | 2023.06.19 |
[백준] 18111 - 마인크래프트 (python) (0) | 2023.06.16 |
[백준] 2583 - 영역구하기 (python) (0) | 2023.06.09 |