"201,500-degree star" IP aggregation

Source: Internet
Author: User

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", &AMP;A[I].A,&AMP;A[I].B,&AMP;A[I].C,&AMP;A[I].D);printf("Case #%d:\n", case++); for(intI=1; i<=m;i++) {intans=0; IP C;scanf("%d.%d.%d.%d", &AMP;C.A,&AMP;C.B,&AMP;C.C,&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.