joj2211

 2211: First of All


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
8s8192K889188Standard

Are you atwitter about this special programming contest? Don't worry-let's first of all talk something about a rather simple problem regarding IPv6 address, which is a new version of the Internet Protocol designed, although inadequate mature, as a successor to IPv4 address; because of its almost astronomical range that can be identified, it is being used to access quite more and more Internet services now. For example, an alternative domain of JOJ, acm.jlu6.edu.cn, points to its IPv6 address:

[2001:da8:b000:6213:208:74ff:fea1:a5eb]

(without square brackets).

For more information about IPv6 address, refer to the following BACKGROUND section excerpted from RFC with slight modification. Surely enough, this section can also be skipped without effect on solving this problem.

BACKGROUND

IPv6 addresses are 128-bit identifiers relative to the 32-bit for IPv4. In IPv4 protocol, the addresses are represented using four decimal numbers separated by period, while in IPv6 protocol there are three conventional forms for representing IPv6 addresses as text strings:

1. The preferred form (standard format) is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal values of the eight 16-bit pieces of the address, such as:
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 and 1080:0:0:0:8:800:200C:417A.
Note that it is not necessary to write the leading zeros in an individual field, but there must be at least one numeral in every field (except for the case described below).

2. Due to some methods of allocating certain styles of IPv6 addresses, it will be common for addresses to contain long strings of zero bits. Therefore, in order to make writing addresses containing zero bits easier, a special syntax (compressed format) is available to compress the zeros: the use of '::' (two colons) indicates one or more groups of 16 bits of zeros. But to avoid potential confusion, the '::' can only appear once in an address, which of course can also be used to compress leading or trailing zeros in an address.

For example, the following addresses:

1080:0:0:0:8:800:200C:417A		a unicast address
FF01:0:0:0:0:0:0:101			a multicast address
0:0:0:0:0:0:0:1				the loop-back address
0:0:0:0:0:0:0:0				the unspecified addresses

May be represented as:

1080::8:800:200C:417A			a unicast address
FF01::101				a multicast address
::1					the loop-back address
::					the unspecified addresses

3. An alternative form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values of the six high-order 16-bit pieces of the address, and the 'd's are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation).

For simplicity, in this problem we merely focus on the first two formats, and meanwhile useful comments and restrictions are given below:

1. In the standard format, the leading zeros in a field must be omitted under the constraint that there must be at least one number in each field.
2. In the compressed format, the use of '::' indicates the maximum span of continuous 16 bits of zeros. In the case that there are two or more sequences culminating the maximum span, the '::' indicates the first of them.

MISSION

Given a compressed IPv6 address, you are to convert it into a standard equivalence: find the '::' and then expand it to a string of continuous 16 bits of zeros separated by ':' in order that the resulting standard address contains exactly 7 ':'s and every adjacent pair of numbers encompasses a ':'.

Input

The input contains several lines, each of which represents a compressed IPv6 address.

Output

Your program should print in the single line the equivalent standard IPv6 address converted from the corresponding compressed IPv6 address given in the input.

Sample Input

1080::8:800:200C:417A
FF01::101

Sample Output

1080:0:0:0:8:800:200C:417A
FF01:0:0:0:0:0:0:101


This problem is used for contest: 31  147  190 


Submit / Problem List / Status / Discuss





z这个是一个让我非常蛋疼的一个问题。首先是题目中允许::之间没有0以至于我做了很久都没有做出来。。。



#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char  str[100];
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%s",str)!=EOF)
    {
        int l=strlen(str);
        int pos=0;
        int sum=0;
        bool temp=true;
for(int i=0;i<l;++i)
{
if(str[i]==':'&&str[i+1]==':')
{
pos=i;temp=false;
for(int j=i+2;j<l;++j)
{
if(str[j]==':')sum++;
}
}
else if(str[i]==':'&&temp)sum++;
}
        if(!temp)
        {
            if(str[0]==':')cout<<0;
            for(int i=0;i<=pos;i++)cout<<str[i];
            for(int i=1;i<7-sum-1;i++)cout<<"0:";
            if(sum!=6)
            cout<<"0:";
            for(int i=pos+2;i<l;i++)cout<<str[i];
            if(pos+2==l)cout<<"0";
            cout<<endl;
        }
        else cout<<str<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值