2015百度之星之-IP聚合
来源:互联网 发布:先锋软件集团现状 编辑:程序博客网 时间:2024/04/29 04:12
IP聚合
Accepts: 138
Submissions: 293
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如:
子网掩码:A.B.C.D
IP 地址:a.b.c.d
网络地址:(A&a).(B&b).(C&c).(D&d)
Input
第一行包含一个整数TTT,(1≤T≤50)(1 \leq T \leq 50)(1≤T≤50)代表测试数据的组数,
接下来TTT组测试数据。每组测试数据包含若干行,
第一行两个正整数N(1≤N≤1000,1≤M≤50),MN(1 \leq N \leq 1000, 1 \leq M \leq 50),MN(1≤N≤1000,1≤M≤50),M。接下来NNN行,每行一个字符串,代表一个 IP 地址,
再接下来MMM行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.DA.B.C.DA.B.C.D的形式,其中A,B,C,DA,B,C,DA,B,C,D均为非负整数,且小于等于255。
Output
对于每组测试数据,输出两行:
第一行输出: “Case #i:” 。iii代表第iii组测试数据。
第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量。
Sample Input
2
5 2
192.168.1.0
192.168.1.101
192.168.2.5
192.168.2.7
202.14.27.235
255.255.255.0
255.255.0.0
4 2
127.127.0.1
10.134.52.0
127.0.10.1
10.134.0.2
235.235.0.0
1.57.16.0
Sample Output
Case #1:
3
2
Case #2:
3
4
做了下这个题,测试下没问题,但是提交到指定的系统却始终是超时
从整体考量确实没有啥大问题,后面就思考了下,唯一的耗时
是在字符串转换上,我想我的先入思路而没深入思考还是很不足,
总是把输入当成似乎必须是字符串一样,后面给出另一个种写法
#include <algorithm>#include <iostream>#include <map>#include <vector>#include <iterator>#include <string.h>#include <stdlib.h>#include <stdio.h>using namespace std;vector<string> split(string s,string delim){ char *ss=const_cast<char *>(s.c_str()); const char *ddelim= delim.data(); char *token = strtok(ss,ddelim); vector<string> vstr; while(token!=NULL){ string stmp = token; vstr.push_back(stmp); token = strtok(NULL,ddelim); } return vstr;}string itostring(unsigned int a){ if(a==0)return "0"; vector<char> vc; while(a){ unsigned int tmp = a%10; vc.push_back(tmp+'0'); a=a/10; } int n=vc.size(); string s(n,'0'); for(int i=n-1;i>=0;--i)s[n-1-i]=vc[i]; return s; }string join(const vector<string> &vs,const string &s){ int n=vs.size(); string restr; for(int i=0;i<n-1;++i)restr=restr+vs[i]+s; restr +=vs[n-1]; return restr;}string ip_add(string s1,string s2){ vector<string> vecs1=split(s1,"."),vecs2=split(s2,"."); int n1=vecs1.size(),n2=vecs2.size(); n1=min(n1,n2); string res; int a1,b1,c; for(int i=0;i<n1;++i){ a1=atoi(vecs1[i].data()); b1=atoi(vecs2[i].data()); c=a1&b1; string stmp=itostring(c); res=res+stmp+"."; } res=res.substr(0,res.size()-1); //cout<< res<<endl; return res;}int main(){ int T; cin>>T; for(int i=0;i<T;++i){ int n,m; cin>>n>>m; vector<string> vn(n),vm(m); int r=0; while(r<n)cin>>vn[r++]; r=0; while(r<m)cin>>vm[r++]; vector<int> vres(m); map<string,int> ms; for(int k=0;k<m;++k){ for(int j=0;j<n;++j){ string tmp=ip_add(vm[k],vn[j]); //cout<<tmp.size()<<tmp<<endl; if(!ms.count(tmp))vres[k]++; ms[tmp]=1; } } cout<<"Case #"<<i+1<<":"<<endl; for(int x=0;x<vres.size();++x)cout<<vres[x]<<endl; } return 0;}
AC
#include <algorithm>#include <iostream>#include <map>#include <vector>#include <iterator>#include <string.h>#define IMIN numeric_limits<int>::min()#define IMAX numeric_limits<int>::max()#define FR(i,n) for(int i=0;i<n;i++)#define CLC(x) memset(x,0,sizeof(x))#define FILL(x,c) memset(x,c,sizeof(x))#define viter vector<int>::const_iterator#define vcter vector<int>::const_iterator#include <set>using namespace std;typedef struct IP_ADD{ int a,b,c,d; //IP_ADD(int _a,int _b,int _c,int _d):a(_a),b(_b),c(_c),d(_c){} bool operator < (const IP_ADD&rhs) const { if(a!=rhs.a) return a<rhs.a; else if(b!=rhs.b) return b<rhs.b; else if(c!=rhs.c) return c<rhs.c; else return d<rhs.d; } friend IP_ADD operator &(IP_ADD& rhs,IP_ADD& lhs) { IP_ADD x; x.a= lhs.a&rhs.a; x.b= lhs.b&rhs.b; x.c= lhs.c&rhs.c; x.d= lhs.d&rhs.d; return x; }}IP;void ip_agg(){ int T,num=1; cin>>T; while(T--){ int m,n; cin>>n>>m; IP_ADD tn[n],tm[m]; for(int j=0;j<n;++j){ char tmp; //cin>>tn[j].a>>tmp>>tn[j].b>>tmp>>tn[j].c>>tmp>>tn[j].d; scanf("%d.%d.%d.%d",&tn[j].a,&tn[j].b,&tn[j].c,&tn[j].d); } vector<int> res(m); cout<<"Case #"<<num++<<":"<<endl; for(int i=0;i<m;++i){ char tmp; //cin>>tm[i].a>>tmp>>tm[i].b>>tmp>>tm[i].c>>tmp>>tm[i].d; scanf("%d.%d.%d.%d",&tm[i].a,&tm[i].b,&tm[i].c,&tm[i].d); set<IP_ADD> ipset; int count=0; for(int k=0;k<n;++k){ IP_ADD now; now=tm[i]&tn[k]; if(!ipset.count(now)){ count++;ipset.insert(now);} } cout<<count<<endl; res[i]=count; } }}int main(){ ip_agg(); return 0;}
- 2015百度之星之-IP聚合
- 百度之星资格赛2015IP聚合
- 【2015百度之星】 IP聚合
- 百度之星2015资格赛 IP聚合
- 2015百度之星 IP聚合
- 百度之星资格赛--IP聚合
- 【百度之星】1003 IP聚合
- [百度之星]资格赛:IP聚合
- bestcoder 百度之星 1003 IP聚合
- 2016 百度之星 IP聚合
- 百度之星2015资格赛1003 IP聚合 模拟题
- BestCoder 2015百度之星资格赛1003 IP聚合
- BestCoder 2015百度之星资格赛1003 IP聚合 -
- 2015百度之星资格赛 IP聚合 1003
- 百度之星2015 资格赛 IP聚合【水坑】
- 百度之星IP聚合(水题map&字符处理)
- [水]百度之星资格赛 1003 IP聚合
- best coder (百度之星) IP聚合
- common lisp 下载设置入门
- LoadRunner脚本回放时错误MERR-27979解决办法
- 对话框程序:模式和非模式
- NJUST 1922 count_prime【容斥原理】
- 【Android学习】使用RxJava完成益智翻牌游戏
- 2015百度之星之-IP聚合
- 第一章 JAVA入门(Main函数简介续)
- Mysql查询优化
- XMPP在iOS上使用-翻译文档
- cPanel下使用WHM配置远程数据库MySQL服务器
- 通过MapReduce JobID 停止(kill)指定任务
- Go语言学习(十一)面向对象编程-类型系统
- 运算符学习—5月9日知识点总结
- cowboy 算法练习