joj2211

来源:互联网 发布:淘宝客服名称修改 编辑:程序博客网 时间:2024/06/11 23:21

 2211: First of All


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE8s8192K889188Standard

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:417Aa unicast addressFF01:0:0:0:0:0:0:101a multicast address0:0:0:0:0:0:0:1the loop-back address0:0:0:0:0:0:0:0the unspecified addresses

May be represented as:

1080::8:800:200C:417Aa unicast addressFF01::101a multicast address::1the 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:417AFF01::101

Sample Output

1080:0:0:0:8:800:200C:417AFF01: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;
}



原创粉丝点击