题目描述
Background
(Friendly reminder: Basic knowledge of IPv6 has appeared multiple times in the preliminary contests of NOIP) Internet Protocol, or IP for short, is the protocol we commonly refer to. The version we currently use is mainly the fourth edition, known as IPv4, which was released by IETF in 1981. Its address length is 32 bits, providing approximately 4.3 billion IP addresses. At that time, no one anticipated that the vast address space of IPv4 would one day be exhausted.
In the 21st century, the booming development of the internet has brought us the convenience of modern life. With the world population exceeding 7 billion, computers and various networked devices have entered millions of households, no longer just tools for scientists in the 1980s. This has led to a contradiction between the growing demand for networked devices and the limited length of IPv4 addresses. Although network address translation technology can temporarily solve the depletion issue by sharing IP addresses, it is clearly not a long-term solution.
The IETF foresaw this issue and released the IPv6 protocol in 1998, which became the default network protocol starting from Windows Vista released by Microsoft in 2006. As the successor to IPv4, IPv6 has an address length of 128 bits, providing 2^128 IP addresses. However, faced with this lengthy address, a network engineer named Xiao Ming, who has poor memory, encountered many difficulties when configuring routing tables. Now, he has turned to you for help, hoping you can write a program to compress IPv6 addresses according to the standard formatting rules.
Problem Description
[IPv6 Format]
In binary, IPv6 is 128 bits long, grouped into sets of 16 bits, separated by colons (:), and can be divided into 8 groups, each represented in 4 hexadecimal digits.
For example, 2001:0db8:0000:0000:0123:4567:89ab:cdef is a valid IPv6 address.
IPv6 addresses can be compressed under certain conditions:
- Leading zeros in each hexadecimal group can be omitted.
For example, the above IPv6 address can be compressed to 2001:db8:0:0:123:4567:89ab:cdef.
- A double colon (
::) can be used to represent one or more consecutive groups of zeros, but it can only appear once.
For example, the above IPv6 address can be compressed to 2001:db8::123:4567:89ab:cdef.
Please help the forgetful network engineer Xiao Ming solve his problem.
[Additional Rules]
-
The input data is a fully expanded IPv6 address, ensuring that it does not contain double colons and that all omitted zeros are filled in.
-
The double colon can only be used once, so we compress the longest group of all zeros.
For example, 2001:0db8:0000:0000:1:0000:0000:0000 is compressed to 2001:db8:0:0:1::, not 2001:db8::1:0:0:0.
- The double colon can only be used once, so when we encounter multiple consecutive groups of all zeros of the same length, we compress the first one.
For example, 2001:0db8:0000:0000:ffff:0000:0000:1 is compressed to 2001:db8::ffff:0:0:1, not 2001:db8:0:0:ffff::1.
- The input IPv6 address may not be compressible, in which case it should be output as is.
Note: The compression rules shown in this problem are the same as the default IPv6 address display method in macOS (Darwin). Windows and Linux do not use :: for compression when there is only one group of all zeros. However, IPv6 addresses compressed with this method can still be correctly recognized by Windows and Linux.
For example, 2001:0db8:ffff:0000:0123:4567:89ab:cdef is compressed to 2001:db8:ffff::123:4567:89ab:cdef by Darwin, while Linux and Windows compress it to 2001:db8:ffff:0:123:4567:89ab:cdef.
Input and Output Formats
Input Format
A string of 39 characters representing a fully expanded IPv6 address.
Output Format
A string representing the compressed IPv6 address.
Sample Input and Output
Sample Input #1
2406:0840:f990:0000:0000:0000:0000:0001
Sample Output #1
2406:840:f990::1
Sample Input #2
2a13:1801:018a:00cf:0100:0000:0000:0000
Sample Output #2
2a13:1801:18a:cf:100::
Sample Input #3
2001:4860:4860:0000:0000:0000:0000:8888
Sample Output #3
2001:4860:4860::8888
Sample Input #4
2001:0db8:0000:0000:0000:0000:0000:0001
Sample Output #4
2001:db8::1
Sample Input #5
0000:0000:0000:0000:0000:0000:0000:0000
Sample Output #5
::
Sample Input #6
0000:0000:0000:0000:0000:0000:0000:0001
Sample Output #6
::1
Sample Input #7
2001:0db8:ffff:0000:0123:4567:89ab:cdef
Sample Output #7
2001:db8:ffff::123:4567:89ab:cdef
Sample Input #8
1234:5678:9abc:def0:1234:5678:9abc:def0
Sample Output #8
1234:5678:9abc:def0:1234:5678:9abc:def0
Sample Input #9
0001:0000:0000:0000:0000:0000:0000:0001
Sample Output #9
1::1
Sample Input #10
0000:0000:0000:0000:0000:0000:0001:0002
Sample Output #10
::1:2
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分