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[];
  9. int T;
  10. cin>>T;
  11. while (T--)
  12. {
  13. scanf("%s",ip6);
  14. int len=strlen(ip6);
  15. int sum=,i,cnt=;
  16. bool flag=;
  17. for (i=; i<=len-; i++)
  18. {
  19. if (ip6[i]==':'&&ip6[i+]!=':') cnt++;
  20. if (ip6[i]==':'&&ip6[i+]==':') flag=;
  21. }
  22. cnt=-cnt;
  23. bool ok=;
  24. for (i=; i<=len-; i++)
  25. {
  26. if (ip6[i-]!=':'&&ip6[i]==':')
  27. {
  28. for (int j=; j<=-sum; j++)
  29. printf("");
  30. for (int j=i-sum; j<=i-; j++)
  31. printf("%c",ip6[j]);
  32. printf(":");
  33. sum=;
  34. }
  35. else if (ip6[i]!=':') sum++;
  36.  
  37. if (ip6[i-]==':'&&ip6[i]==':')
  38. {
  39. if (i!=len-)
  40. for (int z=; z<=cnt; z++)
  41. printf("0000:");
  42. else
  43. {
  44. for (int z=; z<=cnt; z++)
  45. printf("0000:");
  46. printf("");
  47. ok=;
  48. }
  49. }
  50. }
  51. if (ok)
  52. {
  53. for (int j=; j<=-sum; j++)
  54. printf("");
  55. for (int j=i-sum; j<=i-; j++)
  56. printf("%c",ip6[j]);
  57. }
  58. printf("\n");
  59. }
  60. return ;
  61. }

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. 安装WP8 SDK出现“根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内”的解决办法

    今天重装系统了,在安装WP8 SDK时,安装了一小部分就提示“根据当前系统时钟或签名文件中的时间戳验证时要求的证书不在有效期内”的错误. 根据错误提示,貌似跟时间有关,百度了下.果真.把系统时间往前调 ...

  2. 真正的inotify+rsync实时同步 彻底告别同步慢

    真正的inotify+rsync实时同步 彻底告别同步慢       http://www.ttlsa.com/web/let-infotify-rsync-fast/     背景 我们公司在用in ...

  3. Spring MVC Controller中GET方式传过来的中文参数会乱码的问题

    Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...

  4. JAVASCRIPT实现翻页保存已勾选的项目

    <input type="checkbox" name="a" value="1" /><br/> <inpu ...

  5. WordPress 后台禁用Google Open Sans字体,加速网站

    解决方法很简单,安装启用 Disable Google Fonts 或者 Remove Open Sans font Link from WP core 其中之一即可.或者如果你没有使用WP自带的官方 ...

  6. linux乱码问题:LANG变量的秘诀

    对于国内的Linux用户,经常烦恼的一个问题是:系统常常在需要显示中文的时候却显示成了乱码,而由于某些原因,需要英文界面的系统的时候,却苦于系统不能正常输入和显示中文.另外,由于大部分主要Linux发 ...

  7. oracle分页与rownum

    Oracle分页(limit方式的运用) Oracle不支持类似于 MySQL 中的 limit. 但你还是可以rownum来限制返回的结果集的行数. 第一种 select * from a_matr ...

  8. Fedora 17下安装Oracle 10g详细图文教程

    一.硬件要求——内存 & swap & 硬盘 最小内存与swap: 1 GB of RAM & swap 建议内存与swap: 2 GB of RAM & swap [ ...

  9. Linux配置Tomcat(转载)

    转载自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891633.html 安装说明 安装环境:CentOS-6.3安装方式:源码安装 软件:a ...

  10. Unity3d + UGUI 的多分辨率适配

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49471789 1.Canvas的属性配置 2.Canvas Scaler的属性配置 3 ...