HDU Baidu Star (test race) 1003-ip polymerization

Source: Internet
Author: User

Problem Description

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 tt < Span class= "Mord mathit" style= "Margin-right:0.13889em" >t , (1≤T≤50) (1 \leq T \leq) < Span class= "Mopen" > ( 1 ≤ t ≤ 5 0 represents the number of groups of test data,

Next the TTT Group test data. Each set of test data contains several rows,

First row two positive integersN (1≤n≤1000,1≤m≤50), MN (1 \leq N \leq, 1 \leq m \leq), M n (1 ≤ n ≤ 1 0 0 0 , 1 ≤ M ≤ 5 0 ),M 。 Next NNN Line, one string per line, representing an IP address,

And Next MMM Rows, one string per line represents the subnet mask. Both the IP address and the subnet mask are used A.b.c.da. b.c.d A. B . C . D The form of which a,b,c,da,b,c,dA, B, C, D are non-negative integers and are less than or equal to 255.

Output

For each set of test data, output two lines:

The first line of output: "Case #i:". III for Part II I Group test data.

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.

Sample Input
25 2192.168.1.0192.168.1.101192.168.2.5192.168.2.7202.14.27.235255.255.255.0255.255.0.04 2127.127.0.110.134.52.0127.0.10.110.134.0.2235.235.0.01.57.16.0
Sample Output
Case #1:32Case #2:34


The meaning is to let you find in each mask case, n IP address can have several different network address, simply say is to go heavy, thus can think of with set, not very difficult.

#include <cstdio> #include <set>using namespace std;struct ip{int A, B, C, D;         int operator < (const IP &i) const///because a comparison function is required in the Count function in set, write it yourself {if (I.A! = a) return I.A < A;        else if (i.b! = b) return I.B < b;        else if (i.c! = c) return I.C < C;    else return I.D < D; }}address[1010];set<ip> all;int ans;int ip_a[1010], ip_b[1010], ip_c[1010], Ip_d[1010];int ip_ma[60], ip_mb[60],    IP_MC[60], Ip_md[60];int main () {int m, n, T;    scanf ("%d", &t);        for (int icase = 1; icase <= T; ++icase) {scanf ("%d%d", &n, &m);        for (int i = 0; i < n; ++i) scanf ("%d.%d.%d.%d", &ip_a[i], &ip_b[i], &ip_c[i], &ip_d[i]); for (int i = 0; i < m; ++i) scanf ("%d.%d.%d.%d", &ip_ma[i], &ip_mb[i], &ip_mc[i], &ip_        Md[i]);        printf ("Case #%d:\n", icase);          for (int i = 0; i < m; ++i) {all.clear ();  Ans = 0;                for (int j = 0; J < N; ++j) {address[j].a = Ip_a[j] & Ip_ma[i];                address[j].b = Ip_b[j] & Ip_mb[i];                ADDRESS[J].C = Ip_c[j] & Ip_mc[i];                ADDRESS[J].D = Ip_d[j] & Ip_md[i];                    if (!all.count (Address[j])) {ans++;                All.insert (Address[j]);        }} printf ("%d\n", ans); }} return 0;}


HDU Baidu Star (test race) 1003-ip polymerization

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.