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. C#反编译工具 ILSPY-x64可动态调试-君临汉化版

    程序基于著名的ILSpy version 2.1.0.1603 汉化,并增加x64下debugging功能;初衷是网上只有一版是原作者留下的x86版本,实在不想在虚拟机里调试,只有自己动手弄一份x64 ...

  2. Asp.Net Web API VS Asp.Net MVC

    http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...

  3. Python urllib2多进程共享cookies

    如果想多个进程共享同一个cookies,不用每个进程都重新登录,可以就cookies保存到一个文件,然后多个进程直接共享一个锁来实现 1.一个进程登录完成后,把cookies保存到一个文件里面 sel ...

  4. JS和JSP的区别

    最近很多同学在纠结于名词缩写之间的相似性,因此本人也来写一篇,讲讲JS和JSP的区别. SUN首先发展出SERVLET,其功能比较强劲,体系设计也很先进,只是,它输出HTML语句还是采用了老的CGI方 ...

  5. 【转】perl ping检测功能脚本代码

    我的第一个用于生产环境的perl脚本,虽然不是很优秀,但也迈出了扎实的一步 :)领导有任务,给一批IP列表,ping每一台机器,如果没有响应就发邮件通知,通知的邮件需要分开,不能通知一个列表,得一封一 ...

  6. 数据库 mysql 优化器原理

    MySQL查询优化器有几个目标,但是其中最主要的目标是尽可能地使用索引,并且使用最严格的索引来消除尽可能多的数据行. 你的最终目标是提交SELECT语句查找数据行,而不是排除数据行.优化器试图排除数据 ...

  7. Linux内核中的常用宏container_of

    Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...

  8. 【BZOJ 1189】[HNOI2007]紧急疏散evacuate

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...

  9. android项目在eclipse下编译运行的问题

    JDK与电脑系统要匹配,都是32位或者64位: android工程要与JDK相匹配,如果之前的android工程使用的jdk版本较高,则可能出现一些包或者类.方法.属性对应不上而报错,Android ...

  10. JPA2 关于 PagingAndSortingRepository

    And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd): Or --- 等价于 SQL 中的 ...