- 从90分到100分,主要是运行超时的问题,这个我主要使用栈来优化
2
1
2
1.0.0.0/8
2.0.0.0/8
2
10/9
10.128/9
10.0.0.0/8
2
0/1
128/1
0.0.0.0/0
import java.io.*;
import java.util.*;
class Prefix implements Comparable<Prefix> {
private int len;
private long val;
public int getLen() {
return len;
}
public int compareTo(Prefix that) {
if (this.val == that.val) {
return this.len - that.len;
}
if (this.val < that.val) {
return -1;
}
else if (this.val == that.val) {
return 0;
}
else {
return 1;
}
}
public Prefix(Prefix a) {
this.val = a.val;
this.len = a.len - 1;
}
public Prefix(String ip, int len) {
this.val = 0;
this.len = len;
String[] arr = ip.split("\\.");
long base = 1;
for (int i = 3; i >= 0; i--) {
val += base * Integer.parseInt(arr[i]);
base *= 256;
}
}
public boolean isSubsetOf(Prefix that) {
if (this.len < that.len) {
return false;
}
int n = that.len;
long num = ((long)Math.pow(2, n) - 1) << (32 - n);
return (this.val & num) == (that.val & num);
}
public boolean match(Prefix that) {
if (this.len != that.len) {
return false;
}
int n = len - 1;
long num = ((long)Math.pow(2, n) - 1) << (32 - n);
if ((this.val & num) != (that.val & num)) {
return false;
}
long flag = (long) 1 << (32 - len);
if ((this.val & flag) == (that.val & flag)) {
return false;
}
return true;
}
@Override
public String toString() {
long base = 256 * 256 * 256;
long val_cp = this.val;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
sb.append(val_cp / base).append(".");
val_cp -= val_cp / base * base;
base /= 256;
}
sb.setCharAt(sb.length() - 1, '/');
sb.append(this.len);
return sb.toString();
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(f.readLine());
Prefix[] prefixes = new Prefix[n];
for (int i = 0; i < n; i++) {
String prefix = f.readLine();
StringBuilder sb = new StringBuilder();
int len = 0;
int dot_count = 0;
for (char ch : prefix.toCharArray()) {
if (ch == '.') dot_count++;
}
int index = prefix.indexOf('/');
if (index != -1) {
sb.append(prefix.substring(0, index));
len = Integer.parseInt(prefix.substring(index + 1));
}
else {
sb.append(prefix);
len = dot_count * 8 + 8;
}
for (int j = dot_count; j < 3; j++) {
sb.append(".0");
}
prefixes[i] = new Prefix(sb.toString(), len);
}
Arrays.sort(prefixes);
Stack<Prefix> stack = new Stack<>();
stack.push(prefixes[0]);
for (int i = 1; i < prefixes.length; i++)
{
if (!prefixes[i].isSubsetOf(stack.peek())) {
stack.push(prefixes[i]);
}
}
List<Prefix> list = new ArrayList<>();
for (Prefix prefix : stack) {
list.add(prefix);
}
for (int i = 0; i < list.size(); i++) {
Prefix a = list.get(i);
if (i + 1 < list.size())
{
Prefix b = list.get(i + 1);
if (a.getLen() > 0 && a.match(b))
{
list.set(i, new Prefix(a));
list.remove(i + 1);
if (i > 0) i-=2;
else i--;
}
}
}
for (int i = 0; i < list.size(); i++) {
out.println(list.get(i));
}
out.close();
f.close();
}
}
|