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:

 #include<string>
#include<cstring>
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
char ip6[];
int T;
cin>>T;
while (T--)
{
scanf("%s",ip6);
int len=strlen(ip6);
int sum=,i,cnt=;
bool flag=;
for (i=; i<=len-; i++)
{
if (ip6[i]==':'&&ip6[i+]!=':') cnt++;
if (ip6[i]==':'&&ip6[i+]==':') flag=;
}
cnt=-cnt;
bool ok=;
for (i=; i<=len-; i++)
{
if (ip6[i-]!=':'&&ip6[i]==':')
{
for (int j=; j<=-sum; j++)
printf("");
for (int j=i-sum; j<=i-; j++)
printf("%c",ip6[j]);
printf(":");
sum=;
}
else if (ip6[i]!=':') sum++; if (ip6[i-]==':'&&ip6[i]==':')
{
if (i!=len-)
for (int z=; z<=cnt; z++)
printf("0000:");
else
{
for (int z=; z<=cnt; z++)
printf("0000:");
printf("");
ok=;
}
}
}
if (ok)
{
for (int j=; j<=-sum; j++)
printf("");
for (int j=i-sum; j<=i-; j++)
printf("%c",ip6[j]);
}
printf("\n");
}
return ;
}

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

  1. Java IP地址字符串与BigInteger的转换, 支持IPv6

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  2. IP地址字符串与BigInteger的转换

    /**  * Copyright (c) 2010, 新浪网支付中心  *      All rights reserved.  *  * Java IP地址字符串与BigInteger的转换,  * ...

  3. [转载]Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结

    转载:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...

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

    using System; using System.Text.RegularExpressions; namespace MetarCommonSupport { /// <summary&g ...

  5. Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结

    知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式.英语叫做IPv4 numbers-and-dots notation. 如果把210.25.132.181转换 ...

  6. Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结(转)

    原文:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...

  7. ipv6工具类

    package mapreduce.nat; import java.math.BigDecimal; import java.math.BigInteger; import java.net.Ine ...

  8. IPv6地址存储

    import java.util.Arrays; /** * @author: 何其有静 * @date: 2019/4/2 * @description: IPv6地址存储 * https://mp ...

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

    using System; using System.Text.RegularExpressions; namespace SG_VQCDataCollection { /// <summary ...

随机推荐

  1. Kernel Panic常见原因以及解决方法

    Technorati 标签: Kernel Panic 出现原因 1. Linux在中断处理程序中,它不处于任何一个进程上下文,如果使用可能睡眠的函数,则系统调度会被破坏,导致kernel panic ...

  2. 阿里云 centos 环境配置与 django 部署

    1. 免密码登陆 # 本机生成密钥, 并将 pub 复制到阿里云服务器上 $ ssh-keygen -t rsa -P '' # -P表示密码,-P '' 就表示空密码 $ scp ~/.ssh/FI ...

  3. sql server返回插入数据表的id,和插入时间

    假设要插入数据的数据表结构如下

  4. H5全景视频VR视频

    公司的有个专题页面涉及到全景视频展示这个技术点,找到一个相关的库. http://www.utovr.com/sdk/download  这里有个免费的SDK可以下载. 里面也有案例可以看,代码就照着 ...

  5. Beaglebone Back学习三(开发环境搭建)

    开发环境搭建 1 Ubuntu环境搭建 2 Window环境搭建 3 开发板环境搭建 1 Ubuntu环境搭建 (1)安装必要的网络工具 samba nfs tftp vmware-tools sam ...

  6. C中浮点数转字符串

    求浮点数转换成字符串,如何才能获得比较正确的字符串.用printf("%f\n", (float)5); 这种方式转换出来的结果是 5.000000 ,末尾都会带6位小数. 控制精 ...

  7. SVN 提交必填备注Commit

    操作方法:在SVN的Repositories下,找到要配置的项目,在项目目录下找到hooks文件夹,在其下创建pre-commit.bat文件,把下面复制进去就可以了(无需重启,如果改动,保存bat文 ...

  8. 区间型动规--石子归并(Pascal)

    题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使 ...

  9. Lua中cJson的读写

    这里采用的是Lua CJson库,是一个高性能的JSON解析器和编码器,其性能比纯Lua库要高10~20倍.并且Lua Json完全支持UTF-8,无需以来其他非Lua/LuaJit相关包. 环境安装 ...

  10. C#中用ILMerge将所有引用的DLL打成一个DLL文件

    有些文件是必须一起使用的,如果能把多个DLL打包成一个DLL文件,那么引用文件的时候就不需要一个个地去引用,而且每次移动文件的时候也不至于少了哪个必须的DLL文件. 多个DLL文件打包成一个DLL文件 ...