Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[백준] 2064번 IP 주소 #40

Open
nathan29849 opened this issue Oct 19, 2022 · 1 comment
Open

[백준] 2064번 IP 주소 #40

nathan29849 opened this issue Oct 19, 2022 · 1 comment
Assignees

Comments

@nathan29849
Copy link
Member

TITLE

[백준] 2064번 IP 주소

LINK

  • 문제 출처 사이트 : 백준
  • Link

📷 Screenshots

댓글 양식

  • 아래 양식을 복사한 뒤 [shift]+[tab] 2회를 하고 작성하여 주세요
    ### 풀이 언어

    - python/java

    ### 코드

    ```python/java

    ```

    ### 핵심 로직 혹은 자료구조

    - 

    ### 시간 복잡도

    - O(  )

@nathan29849 nathan29849 self-assigned this Oct 19, 2022
@nathan29849
Copy link
Member Author

풀이 언어

  • python

코드

import sys
input = sys.stdin.readline

n = int(input())
ipAddress = []

for _ in range(n):
    address = list(map(int, input().split(".")))
    tmp = ''
    for a in address:
        t = format(a, 'b')
        tmp += '0'*(8-len(t)) + t
    ipAddress.append(tmp)    
ipAddress.sort()
ip, mask = '', ''

for i in range(32):
    if int(ipAddress[0][i]) ^ int(ipAddress[-1][i]) == 1:
        break
    else:
        ip += ipAddress[0][i]
        mask += '1'
        
ip += '0'*(32-i)
mask += '0'*(32-i)
ans1, ans2, tmp1, tmp2 = ['' for _ in range(4)]
for i in range(32):
    tmp1 += ip[i]
    tmp2 += mask[i]
    if (i+1)%8 == 0:
        ans1 += str(int(tmp1, 2)) + '.'
        ans2 += str(int(tmp2, 2)) + '.'
        tmp1, tmp2 = '', ''
        
print(ans1[:-1])
print(ans2[:-1])

핵심 로직 혹은 자료구조

  • 비트마스킹 & 문자열 처리

시간 복잡도

  • O(N)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants