CodeForces 250B Restoring IPv6 解题报告

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

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

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

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

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

    5.   "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 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:1234: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

题意大意是说给定一个ipv6地址的简记形式,让你给它补全输出。一个ipv6地址是由8个小地址组成,每个小地址由‘:‘隔开。简记的规则大致是把地址中的一部分前缀0去掉,或去掉一连串的0,用::来代替。

 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];         //数组str1用来存输入的ipv6地址的简记形式,数组str2用来存输出的ipv6地址的形式
 9     int n,c,i,j,k,cnt,flag;         //cnt用来存小地址的个数,flag用来标记是否为‘:‘,c用来判断小地址的字符数是否达到4个
10     cin>>n;
11     while(n--)
12     {
13         flag=cnt=0;
14         scanf("%s",str1);
15         k=strlen(str1);
16         str1[k]=‘:‘;                //在数组str1后加‘:‘,保证最后一个小地址能计入个数
17         str1[k+1]=‘\0‘;
18         for(i=0; i<=38; i++)        //初始化数组str2
19         {
20             if(i==4||i==9||i==14||i==19||i==24||i==29||i==34)
21                 str2[i]=‘:‘;
22             else
23                 str2[i]=‘0‘;
24         }
25         for(i=0; i<=k; i++)         //计算小地址的个数
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--)           //操作j的位置来控制数组str2的字符
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 }

CodeForces 250B Restoring IPv6 解题报告

时间: 2024-08-06 06:49:35

CodeForces 250B Restoring IPv6 解题报告的相关文章

codeforces 250B Restoring IPv6

题目:大意是在说给定一个ipv6地址的简记形式,让你给它补全输出. 简记的规则大致是把地址中的一部分0去掉,其中还包括一连串的0,此时可用::来代替. 方法:首先记录给定的字符串中的:的个数,让后就能确定::中间要补全的0000的个数,然后对于每个小地址(例如bfd),补全失去的0就好了,这时候可以使用printf输出补0的功能,即:printf("%04s",s); 注意:本来每个字符串中:的个数是不能超过7个的,但是会出现::连用,就可能出现8个,这样对于计算::之间补全0000的

codeforces 591A. Wizards&#39; Duel 解题报告

题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be-Named 分别在走廊末端,各发射自己的impulse,其中Harry 的 impulse 速度为 p 米/s,He-...-Named (这个名字太长,姑且写成这样)为 q米/s.然后相遇之后各自回到它们的主人身边,再发射,问第二次相遇的时候,Harry的impulse 离他的位置距离是多少.

Codeforces Round #277.5 解题报告

又熬夜刷了cf,今天比正常多一题,比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack,写写解题报告吧= =! 解题报告如下: A题:选择排序直接搞,因为不要求最优交换次数,代码: #include <iostream> #include <algorithm> #include <cstdio> #include <memory.h> #include <vector> #include <stack> #

codeforces 495B. Modular Equations 解题报告

题目链接:http://codeforces.com/problemset/problem/495/B 题目意思:给出两个非负整数a,b,求出符合这个等式      的所有x,并输出 x 的数量,如果 x 有无限多个,那么输出 infinity. 想了半个多小时......有个地方想遗漏了. a mod x == b,等价于  a = k*x + b.设 mul = a - b,那么 k*x = mul,然后就不断枚举 mul 的因子,即 kx = mul.由于 mod 出来的结果为 b,那么

codeforces 495A. Digital Counter 解题报告

题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差点就想求救 XX 兽了,最终还是打住,尽量不要依赖人嘛.今天终于想到了,大感动 ~_~ 理解能力有待提高... One of the sticks of the counter was broken    这句话有一点误导人的成分,我刚开始就以为只有一条 stick 坏了= =,再看这句 becau

codeforces 577B. Modulo Sum 解题报告

题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中选出一些数(不能为空),使得这些数的总和能整除 m . 实不相瞒,完全没想法...看题解,有个地方看都看不懂: n > m的情况.求助乌冬子,连带被批英语水皮 >___<.还是谢谢他啦,一步一步引导我. 貌似挺多人也有这个疑惑的.他说那个是特例优化,原谅我懒,直接摘抄吧~ 首先知道一些参数.

codeforces 496B. Secret Combination 解题报告

题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个位数)   2.循环右移(最右边那个数字去到第一位上).问经过若个两种操作的组合后,得到的最小数值为多少. 我一开始用了vector来做= =,没有考虑到循环右移的情况.以为每一位从1加到9之后,找出最小的那个就可以..... 留个纪念(错误代码,别学,如果没有循环右移的限制,这个是对的) 1 #incl

codeforces 582A. GCD Table 解题报告

题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com/blog/entry/20692 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm>

Codeforces Round#320 Div2 解题报告

Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Finding Team Member codeforces 579C A Problem about Polyline codeforces 579D "Or" Game codeforces 579E Weakness and Poorness codeforces 579F LCS Aga