Shorten IPv6 Address
题目来源于牛客网2019牛客暑期多校训练营(第六场)。
题目描述
You are given an IPv6 address which is a 128-bit binary string. Please determine its shortest representation according to the following rules:
Express the address in hexadecimal representation and use a colon ‘:’ to split every four binary digits. Every four digits are called a field. For example, ‘0000:0000:0123:4567:89ab:0000:0000:0000’.
Leading zeros in a field can be omitted. For example, the above IPv6 address can be shortened to ‘0:0:123:4567:89ab:0:0:0’.
Consecutive zero fields (with colons near them) consisting of at least two fields can be replaced by a double colon ‘::’. Besides, no more than one double colon can be used in an address. For example, the above IPv6 address can be shortened to ‘0:0:123:4567:89ab::’ or ‘::123:4567:89ab:0:0:0’, but not ‘::123:4567:89ab::’.
If there are multiple shortest forms of the same length, use the lexicographically smallest one.
If the above rules conflict with rules in the real world, please refer to the rules mentioned in this problem.
输入描述
There are multiple test cases. The first line contains an integer T ( 1 ≤ T ≤ 1000 ) T (1 \leq T \leq 1000) T(1≤T≤1000), indicating the number of test cases. Test cases are given in the following.
Each test case consists of only one line, containing a 128-bit binary string.
输出描述
For each test case, output “Case #x: y” in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer to this test case.
样例
输入
3
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001101000101011001111000100110101011000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001100000000000000000000000000000000000000000000000001000101011001111000100110101011
输出
Case #1: ::
Case #2: 0:0:123:4567:89ab::
Case #3: 0:0:123::4567:89ab
题解
这道题需要完成的任务是将以128位二进制表示的ipv6地址转换成8组4位16进制,并按照规则进行缩短。
这道题我的大致流程为“读入二进制字符串->转换为数字->尝试省去不同位置的连续0->输出最符合要求的结果”。
代码
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i++) {
//读入二进制数字,并对连续0进行计数
int[] address = new int[8];//用于存储数字形式的地址
char[] in = br.readLine().toCharArray();
List<Case> list = new ArrayList<>();
int zero_count = 0;//连续0的数量
for (int j = 0; j < 8; j++) {
address[j] = toInt(in, j * 16, j * 16 + 16);
if (address[j] != 0) {
if (zero_count >= 2) {
list.add(new Case(j - 1, zero_count));
}
zero_count = 0;
} else {
zero_count = zero_count + 1;
if (zero_count >= 2 && j == 7) {
list.add(new Case(7, zero_count));
}
}
}
//在所有情况中优先选择最短的,同样长度选择字典序较低的
System.out.print("Case #" + i + ": ");
TreeSet<String> set = new TreeSet<>((o1, o2) ->
o1.length() != o2.length() ? o1.length() - o2.length() : o1.compareTo(o2)
);
list.add(new Case(0, 0));//防止下一行NPE
for (Case p : list) {
StringBuilder s = new StringBuilder();
for (int j = 0; j < 8; j++) {
int max_zero_begin = p.end - p.count + 1;
if (p.count > 0 && j == max_zero_begin) {
//省略部分的开头
if (p.end == 7) s.append("::");
else s.append(":");
} else if (j < max_zero_begin || j > p.end) {
//正常输出
if (j != 0) s.append(":");
s.append(Integer.toHexString(address[j]));
}
}
set.add(s.toString());
}
System.out.println(set.first());//输出最由解
}
}
/**
* 将二进制表示数组的[begin,end)部分转换为数字
*/
static int toInt(char[] in, int begin, int end) {
int ret = 0;
for (int i = begin; i < end; i++) {
ret *= 2;
ret += in[i] - '0';
}
return ret;
}
public static class Case {
int end, count;
Case(int end, int count) {
this.end = end;
this.count = count;
}
}
}