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:
#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(字符串处理)的更多相关文章
- 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 ...
- IP地址字符串与BigInteger的转换
/** * Copyright (c) 2010, 新浪网支付中心 * All rights reserved. * * Java IP地址字符串与BigInteger的转换, * ...
- [转载]Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结
转载:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...
- C# 正则表达式 判断各种字符串(如手机号)
using System; using System.Text.RegularExpressions; namespace MetarCommonSupport { /// <summary&g ...
- Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结
知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式.英语叫做IPv4 numbers-and-dots notation. 如果把210.25.132.181转换 ...
- Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结(转)
原文:http://blog.csdn.net/ithomer/article/details/6100734 知识背景: 210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式 ...
- ipv6工具类
package mapreduce.nat; import java.math.BigDecimal; import java.math.BigInteger; import java.net.Ine ...
- IPv6地址存储
import java.util.Arrays; /** * @author: 何其有静 * @date: 2019/4/2 * @description: IPv6地址存储 * https://mp ...
- C# 验证类(使用正则表达式 验证文本框)
using System; using System.Text.RegularExpressions; namespace SG_VQCDataCollection { /// <summary ...
随机推荐
- C语言 SDK编程之通用控件的使用--ListView
一.ListView控件属于通用控件CONTROL中的一种,在SDK编程方式时要使用通用控件 必须包含comctl32.dll,所以代码中要有头文件: commctrl.h 导入库:comctl32. ...
- 一些达成共识的JavaScript编码风格约定
如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...
- gulp 不是内部或者外部命令 或者 webpack 不是内部或者外部命令
gulp安装也正常,但是就是查看gulp -v的时候报错,原因:缺少系统变量PATH或者PATH变量错误 提示:这个系统变量PATH,直接追加就好(多个变量值用分号;隔开),不要删除已经有的系统变量P ...
- PHP版3DES加解密类
<?php /** * * PHP版3DES加解密类 * * 可与java的3DES(DESede)加密方式兼容 * * @Author:蓝凤(ilanfeng.com) * * @versio ...
- google map 点与点画线
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nrm —— 快速切换 NPM 源 (附带测速功能)
以前我们介绍过cnpmjs.org和最近推出的淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,然后各个镜像各自都可能有少数包暂时没有同步 ...
- Pandas简易入门(三)
本节主要介绍一下Pandas的数据结构,本文引用的网址:https://www.dataquest.io/mission/146/pandas-internals-series 本文所使用的数据来自于 ...
- 七、mysql索引选择
.myisam,bdb,innodb,memory 单表至少支持16个索引 .create index id_index on emp (id) 为emp表创建一个名为id_index的id字段的索引 ...
- frame 之间访问
index.asp代码 <frameset rows="50,*,20" cols="*" frameborder="no" bord ...
- random note
今天才慢慢意识到,什么才是学习,(以思考解决问题为驱动),埋头刷分只是方法,不是目的和原动力. 既然准备读研,就要慢慢去了解研究生的生活学习方式是什么样的,涉及到哪些方面. 读研之前要选好方向,但是现 ...