IP Aggregation Topics
Today's world, the network has been everywhere, the small bear because made a mistake, when the degree of the company's network administrator, he has a large number of IP list, the small bear want to know in a fixed subnet mask, how many network address. The network address is the result of the bitwise AND operation of the subnet mask and IP address, for example:
Subnet Mask: a.b.c.d
IP Address: a.b.c.d
Network address: (A&A). (b/b). (c&c). (d&d)
Input
The first line contains an integer t, (1≤T≤) Number of groups representing the test data
Next the T group tests the data. Each set of test data contains several rows,
First row two positive integers N ,M (1≤N ≤ +,1≤M ≤ -) 。 Next n lines, one string per line, representing an IP address
Next m lines, one string per line represents the subnet mask. Both the IP address and the subnet mask are in the form of a.b.c.d, where a,b,c,d are non-negative integers and less than or equal to 255.
Output
For each set of test data, output two lines:
The first line of output: "Case #i:". I represents the test data for group I.
The second line outputs the results of the test data, and for each subnet mask in each set of data, the number of network addresses that are output under this subnet mask.
Exercises
This problem is to take and the back of the sequence, see the number of different numbers, the specific operation can be set, very convenient.
Code
#include <iostream>#include <cstring>#include <cstdio>#include <set>using namespace STD;intT,n,m;structip{intA,b,c,d;BOOL operator< (ConstIP &x)Const{if(x.a!=a)returnx.a<a;Else if(x.b!=b)returnx.b<b;Else if(X.C!=C)returnx.c<c;Else returnx.d<d; } IPoperator& (IP &x) {IP y; y.a=x.a&a; y.b=x.b&b; y.c=x.c&c; y.d=x.d&d;returnY }}; set<IP>D;ip a[1005];intMain () {intCase=1;scanf("%d", &t); while(t--) {memsetA0,sizeof(a));scanf("%d%d", &n,&m); for(intI=1; i<=n;i++)scanf("%d.%d.%d.%d", &A[I].A,&A[I].B,&A[I].C,&A[I].D);printf("Case #%d:\n", case++); for(intI=1; i<=m;i++) {intans=0; IP C;scanf("%d.%d.%d.%d", &C.A,&C.B,&C.C,&C.D); D.clear (); for(intI=1; i<=n;i++) {IP F; f=a[i]&c;if(!d.count (f)) {ans++; D.insert (f); } }printf("%d\n", ans); } }return 0;}
"201,500-degree star" IP aggregation