Restoring IPv6 (Codeforces 250B)
Tags: Implement, strings

An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: "0124:5678:90ab:cdef:0124:5678:90ab:cdef". We'll call such format of recording an IPv6-address full.Besides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: "a56f:00d3:0000:0124:0001:f19a:1000:0000"  →  "a56f:d3:0:0124:01:f19a:1000:00". There are more ways to shorten zeroes in this IPv6 address.Some IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to "::". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. You can see examples of zero block shortenings below:  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::";  "a56f:0000:0000:0124:0001:0000:1234:0ff0"  →  "a56f::0124:0001:0000:1234:0ff0";  "a56f:0000:0000:0000:0001:0000:1234:0ff0"  →  "a56f:0000::0000:0001:0000:1234:0ff0";  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::0000";  "0000:0000:0000:0000:0000:0000:0000:0000"  →  "::". It is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters "::" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon.The format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short.You've got several short records of IPv6 addresses. Restore their full record.

Input: The first line contains a single integer n — the number of records to restore (1 ≤ n ≤ 100).Each of the following n lines contains a string — the short IPv6 addresses. Each string only consists of string characters "0123456789abcdef:".It is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.

Output: For each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input.


Sample Inputs:
6
a56f:d3:0:0124:01:f19a:1000:00
a56f:00d3:0000:0124:0001::
a56f::0124:0001:0000:1234:0ff0
a56f:0000::0000:0001:0000:1234:0ff0
::
0ea::4d:f4:6:0

Sample Outputs:
a56f:00d3:0000:0124:0001:f19a:1000:0000
a56f:00d3:0000:0124:0001:0000:0000:0000
a56f:0000:0000:0124:0001:0000:1234:0ff0
a56f:0000:0000:0000:0001:0000:1234:0ff0
0000:0000:0000:0000:0000:0000:0000:0000
00ea:0000:0000:0000:004d:00f4:0006:0000

Solution (Python):
T = int(input())
while T:
    ip = input()
    blocks = ip.split(':')
    for i in range(len(ip) - 1):
        if ip[i] == ':' and ip[i + 1] == ':':
            if len(blocks) == 9:
                ip = ip[:i] + ip[i + 1:]
            ip = ip[:i] + (8 - len(blocks)) * ':' + ip[i:]
            break
    blocks = ip.split(':')
    ans = ''
    for b in blocks:
        b = '0' * (4 - len(b)) + b
        ans += b
        ans += ':'
    print(ans[:-1])
    T -= 1

Solution (Python):
n = int(input())
answer = []
for y in range(n):
    full = ''
    shorten = input()
    col_n = 0
    for x in range(len(shorten)):
        if shorten[x] == ':':
            col_n += 1
    if col_n < 7 or shorten.find('::') != -1:
        shorten = shorten.replace('::', ':' + '0:' * (8 - col_n))
    shorten = shorten.split(':')
    for x in range(len(shorten)):
        full += ':' + '0' * (4 - len(shorten[x])) + shorten[x]
    answer.append(full)
for y in range(len(answer)):
    print(answer[y][1:len(answer[y])])

Solution (Python):
def solve(ip):
    ch = ip.split(':')
    n = len(ch)
    if ch[n - 1] == '' and n - 2 >= 0 and (ch[n - 2] == ''):
        ch.pop(n - 1)
    if ch[0] == '' and n > 1 and (ch[1] == ''):
        ch.pop(0)
    extra = 8 - len(ch)
    ans = []
    for i in range(len(ch)):
        if ch[i] != '':
            while len(ch[i]) < 4:
                ch[i] = '0' + ch[i]
            ans.append(ch[i])
        else:
            ans.append('0000')
            while extra > 0:
                ans.append('0000')
                extra -= 1
    ret = ''
    for i in range(len(ans)):
        ret += ans[i]
        if i != len(ans) - 1:
            ret += ':'
    return ret
n = int(input())
for i in range(n):
    s = input()
    print(solve(s))