Best Coder (Baidu Star) IP aggregation

Source: Internet
Author: User

IP Aggregationaccepts:140submissions:297Time limit:2000/1000 MS (java/others)Memory limit:65536/65536 K (java/others) 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 equal to the subnet mask and IP address bitwise AND operation results, 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 ≤ 50 ) Represents the number of groups of test data, and the next T Group test data. Each group of test data contains several rows, the first row two positive integers N (1≤N ≤ +,1≤M ≤ -),M 。 Next N Line, a string per line, representing an IP address, and then M Rows, one string per line represents the subnet mask. Both the IP address and the subnet mask are used a B C . D The form of which a 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:". i On behalf of section 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 InputCopy
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 OutputCopy
Case #1:32Case #2:3

4

AC Code:

#include <stdio.h> #include <string.h>int main () {int t,n,m,count,num=0;int str[1005][5];int a[1005],b[ 1005],c[1005],d[1005];int a[55],b[55],c[55],d[55];int visit[1005];scanf ("%d", &t), while (t--) {num++;scanf ("%d% D ", &n,&m); for (int i=0;i<n;i++) {scanf ("%d.%d.%d.%d ", &a[i],&b[i],&c[i],&d[i]);} for (int i=0;i<m;i++) {scanf ("%d.%d.%d.%d", &a[i],&b[i],&c[i],&d[i]);} printf ("Case #%d:\n", num), for (int i=0;i<m;i++) {count=0;memset (visit,0,sizeof (visit)), and for (int j=0;j<n;j++) {    int x,k=0;str[j][1]=a[j]&a[i];str[j][2]=b[j]&b[i]; str[j][3]=c[j]&c[i];str[j][4]=d[j]&d[i];//printf ("***********%s\n", Str[j]); for (int s=0;s<j;s++) {if ( STR[S][1]==STR[J][1]&AMP;&AMP;STR[S][2]==STR[J][2]&AMP;&AMP;STR[S][3]==STR[J][3]&AMP;&AMP;STR[S][4]==STR[J][4]                           &&!visit[s]) {count++; visit[s]=1;}}} printf ("%d\n", N-count);}} return 0;}


Error code: Feel the same as above, but the character processing time do not know where the error, not yet found that welcome to point out ...

#include <stdio.h> #include <string.h>int main () {int T,n,m,count,num=0;char str[1005][15];int a[1005],b[ 1005],c[1005],d[1005];int a[55],b[55],c[55],d[55];int visit[1005];scanf ("%d", &t), while (t--) {num++;scanf ("%d% D ", &n,&m); for (int i=0;i<n;i++) {scanf ("%d.%d.%d.%d ", &a[i],&b[i],&c[i],&d[i]);} for (int i=0;i<m;i++) {scanf ("%d.%d.%d.%d", &a[i],&b[i],&c[i],&d[i]);} printf ("Case #%d:\n", num), for (int i=0;i<m;i++) {count=0;memset (visit,0,sizeof (visit)), and for (int j=0;j<n;j++) {    int x,k=0;x=a[j]&a[i];str[j][k++]=x/100+ ' 0 '; str[j][k++]=x%100/10+ ' 0 '; str[j][k++]=x%10+ ' 0 ';x=b[j]&B[i]; str[j][k++]=x/100+ ' 0 '; str[j][k++]=x%100/10+ ' 0 '; str[j][k++]=x%10+ ' 0 '; x=c[j]&c[i];str[j][k++]=x/100+ ' 0 '; str[ j][k++]=x%100/10+ ' 0 '; str[j][k++]=x%10+ ' 0 '; x=d[j]&d[i];str[j][k++]=x/100+ ' 0 '; str[j][k++]=x%100/10+ ' 0 '; str[j ][k++]=x%10+ ' 0 ';//printf ("***********%s\n", Str[j]); for (int s=0;s<j;s++) {if (strcmp (Str[s],str[j]) ==0&        &!visit[s]) {count++;                   visit[s]=1;}}} printf ("%d\n", N-count);}} return 0;} S=nx^2/[4tan (π/n)]


Best Coder (Baidu 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.