I want to ask the subject to a given minimum prefix list contains the equivalent number of IP prefixes prefix list.
The first is how to store the prefix list. With a long long storage IP address, a prefix length reload, encapsulated in a structure where \ (<ipNum, len> \) , to facilitate later sorting operation. IP prefix has three input format, to discuss the situation somewhat divided.
Followed by \ (ipNum \) as a first key, \ (len \) is the second key in ascending order.
Then consider removing the IP prefix matching set of other IP prefix contained. Before considering the last match set range \ (mmax \) , the order of traversal just fine. The remaining IP sequence listing by the presence of a static until the list.
Finally adjacent combinable IP prefix merger, in fact, the last bit prefix length of 0 and 1, can be exactly the same as before.
Brush up on the list of insertion and deletion operations.
#include <bits/stdc++.h>
typedef long long LL;
const int maxn = 1000000;
using namespace std;
struct tIP
{
LL ipNum;
int len;
int before, next;
tIP()
{
before = next = -1;
}
bool operator < (const tIP &y) const
{
if(ipNum == y.ipNum)
return len < y.len;
return ipNum < y.ipNum;
}
void show()
{
LL num[5];
LL temp = ipNum;
for (int i = 4; i >= 1; i--)
{
num[i] = temp % 256;
temp /= 256;
}
for (int i = 1; i <=4; i++)
{
printf("%lld", num[i]);
if (i == 4)
printf("/");
else
printf(".");
}
printf("%d\n", len);
}
};
tIP ip[maxn+10];
LL getMMax(tIP iip)
{
LL temp = (1LL << (32-iip.len)) - 1;
return iip.ipNum | temp;
}
int main()
{
int n;
scanf("%d", &n);
char s[30];
for (int id = 1, slash, dotCnt, style; id <= n; id++)
{
slash = 0;
dotCnt = 0;
scanf("%s", s + 1);
for (int i = 1; s[i] != '\0'; i++)
{
if (s[i] == '/')
slash = 1;
if (s[i] == '.')
dotCnt ++;
}
if (slash == 1 && dotCnt == 3)
style = 1;
else if (slash == 1 && dotCnt < 3)
style = 2;
else
style = 3;
LL num[5];
memset(num, 0, sizeof(num));
if (style == 1 || style == 2)
{
for (int i = 1, temp = 0, numCnt = 1; ; i++)
{
if (s[i] == '.' || s[i] == '/')
{
num[numCnt++] = temp * 1LL;
temp = 0;
}
else if (s[i] == '\0')
{
ip[id].len = temp;
break;
}
else
{
temp = temp * 10 + s[i] - '0';
}
}
}
else
{
for (int i = 1, temp = 0, numCnt = 1; ; i++)
{
if (s[i] == '.')
{
num[numCnt++] = temp * 1LL;
temp = 0;
}
else if (s[i] == '\0')
{
num[numCnt++] = temp * 1LL;
ip[id].len = (numCnt-1) * 8;
break;
}
else
{
temp = temp * 10 + s[i] - '0';
}
}
}
LL ans = 0;
for (int i = 1; i <= 4; i++)
{
ans = ans * 256 + num[i];
}
ip[id].ipNum = ans;
}
sort(ip + 1, ip + 1 + n);
LL mmax = -1;
int st = 0, en = n + 1;
ip[st].before = -1;
ip[st].next = en;
ip[en].before = st;
ip[en].next = -1;
for (int id = 1, prev = 0; id <= n; id++)
{
if (ip[id].ipNum > mmax)
{
ip[id].before = prev;
ip[id].next = en;
ip[prev].next = ip[en].before = id;
prev = id;
mmax = getMMax(ip[id]);
}
}
int pNow = ip[0].next;
while (pNow != en)
{
int p1 = pNow, p2 = ip[pNow].before;
if (p2 == 0)
pNow = ip[pNow].next;
else
{
if (ip[p1].len == ip[p2].len &&
(ip[p2].ipNum & (1LL << (32-ip[p2].len))) == 0 &&
(ip[p2].ipNum | (1LL << (32-ip[p2].len))) == ip[p1].ipNum)
{
ip[p1].before = ip[p2].before;
ip[ip[p1].before].next = p1;
ip[p1].ipNum = ip[p2].ipNum;
ip[p1].len --;
}
else
{
pNow = ip[pNow].next;
}
}
}
pNow = ip[0].next;
while (pNow != en)
{
ip[pNow].show();
pNow = ip[pNow].next;
}
return 0;
}