Codeforces 250b restoring IPv6 solution report

Source: Internet
Author: User

Description

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 shoshould 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" comment → blank "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 "::". 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:

  • 1. "a56f: 00d3: 0000: 0124: 0001: 0000: 0000: 0000" comment → answer "a56f: 00d3: 0000: 0124: 0001 ::";

    2. "a56f: 0000: 0000: 0124: 0001: 0000: 0ff0" placement → fill "a56f: 1234: 0124: 0001: 0000: 0ff0 ";

    3. "a56f: 0000: 0000: 0000: 0001: 0000: 0ff0" placement → timeout "a56f: 1234: 0000: 0000: 0001: 0000: 0ff0 ";

    4. "a56f: 00d3: 0000: 0124: 0001: 0000: 0000: 0000" comment → answer "a56f: 00d3: 0000: 0124: 0001: 0000 ";

5. "0000: 0000: 0000: 0000: 0000: 0000: 0000: 0000" Success → success "::".

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 integerN-The number of records to restore (1 limit ≤ limitNLimit ≤ limit 100 ).

Each of the followingNLines contains a string-the short IPv6 addresses. Each string only consists of string characters "0123456789 abcdef :".

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 Input

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: 0ff0

::

0ea: 4d: F4: 6: 0

Sample output

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

Question:The general idea is to give an IPv6 address a simplified form so that you can complete the output.An IPv6 address consists of eight small addresses separated.The rule of note is to remove a part of the prefix 0 in the address, or remove a series of 0, which is replaced.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <algorithm> 4 # include <cstring> 5 using namespace STD; 6 int main () 7 {8 char str1 [50], str2 [50]; // The str1 array is used to save the entered IPv6 address in the simplified form, array str2 is used to store the output IPv6 address in the form of 9 int N, C, I, J, K, CNT, flag; // CNT is used to store the number of small addresses, flag is used to mark whether it is ':'. C is used to determine whether the number of characters in a small address reaches 4 10 CIN> N; 11 while (n --) 12 {13 flag = CNT = 0; 14 scanf ("% s", str1); 15 k = strlen (str1); 16 str1 [k] = ':'; // Add ':' After array str1 to ensure that the last small address can be counted into number 17 str1 [K + 1] = '\ 0'; 18 for (I = 0; I <= 38; I ++) // initialize the array str219 {20 if (I = 4 | I = 9 | I = 14 | I = 19 | I = 24 | I = 29 | I = 34) 21 str2 [I] = ':'; 22 else23 str2 [I] = '0'; 24} 25 for (I = 0; I <= K; I ++) // calculate the number of small addresses 26 {27 if (str1 [I]! = ':') 28 flag = 1; 29 else if (flag = 1) 30 {31 CNT ++; 32 flag = 0; 33} 34} 35 C = 0; 36 J = 38; 37 for (I = K-1; I> = 0; I --) // operate the position of J to control the str2 character 38 {39 if (str1 [I]! = ':') 40 {41 c ++; 42 str2 [j --] = str1 [I]; 43} 44 If (str1 [I] = ':') 45 {46 If (C = 4) 47 j --; 48 else if (C = 3) 49 J = J-2; 50 else if (C = 2) 51 J = J-3; 52 else if (C = 1) 53 J = j-4; 54 c = 0; 55} 56 If (str1 [I] = ': '& str1 [I-1] =': ') 57 {58 J = J-(8-cnt) * 4 + (8-cnt); 59 I --; 60} 61} 62 for (I = 0; I <= 37; I ++) 63 cout <str2 [I]; 64 cout <str2 [38] <Endl; 65} 66 return 0; 67}
View code

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.