CodeForces250B——Restoring IPv6(字符串处理)

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:
"a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::";
"a56f:0000:0000:0124:0001:0000:1234:0ff0"  →  "a56f::0124:0001:0000:1234:0ff0";
"a56f:0000:0000:0000:0001:0000:1234:0ff0"  →  "a56f:0000::0000:0001:0000:1234:0ff0";
"a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::0000";
"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
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
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的全写。样例简单易懂!

解题思路:

    1)根据‘:‘的个数和‘::’的个数 可以判断已经存在的字符段个数。

    2)总段数为8,可以求出需要补完的字符段个数(0000)

    3)遍历整个字符串并输出答案。(遇到‘::‘则输出需要补全的0000,注意"::"是否在字符串末尾!)

Code:

 1 #include<string>
 2 #include<cstring>
 3 #include<stdio.h>
 4 #include<iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     char ip6[1000];
 9     int T;
10     cin>>T;
11     while (T--)
12     {
13         scanf("%s",ip6);
14         int len=strlen(ip6);
15         int sum=0,i,cnt=0;
16         bool flag=0;
17         for (i=0; i<=len-1; i++)
18         {
19             if (ip6[i]==‘:‘&&ip6[i+1]!=‘:‘) cnt++;
20             if (ip6[i]==‘:‘&&ip6[i+1]==‘:‘) flag=1;
21         }
22         cnt=7-cnt;
23         bool ok=1;
24         for (i=0; i<=len-1; i++)
25         {
26             if (ip6[i-1]!=‘:‘&&ip6[i]==‘:‘)
27             {
28                 for (int j=1; j<=4-sum; j++)
29                     printf("0");
30                 for (int j=i-sum; j<=i-1; j++)
31                     printf("%c",ip6[j]);
32                 printf(":");
33                 sum=0;
34             }
35             else if (ip6[i]!=‘:‘) sum++;
36
37             if (ip6[i-1]==‘:‘&&ip6[i]==‘:‘)
38             {
39                 if (i!=len-1)
40                     for (int z=1; z<=cnt; z++)
41                         printf("0000:");
42                 else
43                 {
44                     for (int z=1; z<=cnt; z++)
45                         printf("0000:");
46                     printf("0000");
47                     ok=0;
48                 }
49             }
50         }
51         if (ok)
52         {
53             for (int j=1; j<=4-sum; j++)
54                 printf("0");
55             for (int j=i-sum; j<=i-1; j++)
56                 printf("%c",ip6[j]);
57         }
58         printf("\n");
59     }
60     return 0;
61 }

CodeForces250B——Restoring IPv6(字符串处理)

时间: 2024-10-22 18:09:46

CodeForces250B——Restoring IPv6(字符串处理)的相关文章

codeforces 250B Restoring IPv6

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

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 o

C#判断各种字符串(如手机号)

[csharp] view plaincopy using System; using System.Text.RegularExpressions; namespace MetarCommonSupport { /// <summary> /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查 /// </summary> public class MetarnetRegex { private static MetarnetRegex instance = 

IP地址字符串与BigInteger的转换

/**  * Copyright (c) 2010, 新浪网支付中心  *      All rights reserved.  *  * Java IP地址字符串与BigInteger的转换,  * 支持IPv6  *  */ import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; /**  * IP转换大数进行比较工具  *  * @author [emai

C# 正则表达式 判断各种字符串(如手机号)

1 using System; 2 using System.Text.RegularExpressions; 3 namespace MetarCommonSupport 4 { 5 /// <summary> 6 /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查 7 /// </summary> 8 public class MetarnetRegex 9 { 10 11 private static MetarnetRegex instance =

ipv6工具类

package mapreduce.nat; import java.math.BigDecimal; import java.math.BigInteger; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.ut

Linux统系统开发11 Socket API编程2 多进程 多线程 高并发处理

[本文谢绝转载原文来自http://990487026.blog.51cto.com] <纲要> Linux统系统开发11 Socket API编程2 多进程 多线程 高并发处理 UDP服务器 客户端最小模型,处理字符转大写 TCP 多进程并发服务器模型,为每个客户端开启一个进程: TCP 多线程服务器模型,使用wrap函数封装 作业: ---------------------------------------------------- UDP服务器 客户端最小模型,处理字符转大写 [em

Linux统系统开发12 Socket API编程3 TCP状态转换 多路IO高并发select poll epoll udp组播 线程池

[本文谢绝转载原文来自http://990487026.blog.51cto.com] Linux统系统开发12 Socket API编程3 TCP状态转换 多路IO高并发select  poll  epoll udp组播 线程池 TCP 11种状态理解: 1,客户端正常发起关闭请求 2,客户端与服务端同时发起关闭请求 3,FIN_WAIT1直接转变TIME_WAIT 4,客户端接收来自服务器的关闭连接请求 多路IO转接服务器: select模型 poll模型 epoll模型 udp组播模型 线

C# 验证类(使用正则表达式 验证文本框)

using System; using System.Text.RegularExpressions; namespace SG_VQCDataCollection { /// <summary> /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查 /// </summary> public class MetarnetRegex { private static MetarnetRegex instance = null; public static Me