关注
我是把ip地址转成32位的二进制,然后XOR,看到比较短的那个掩码的位数是不是都是0,但是只过了60多,不知道为什么。
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <bitset>
using namespace std;
ostringstream ostr;
struct ip_addr {
bitset<32> s1,s2,s3,s4;
unsigned int l;
bitset<32> bin;
};
int main(){
int n= 0;
cin >> n;
vector<ip_addr> ips;
vector<string> ipstring;
set<int> idx;
for(int i = 0; i < n; i++){
ip_addr ip;
unsigned int s1,s2,s3,s4,l;
scanf("%d.%d.%d.%d/%d",&s1,&s2,&s3,&s4,&ip.l);
ip.s1 = bitset<32>(s1);
ip.s2 = bitset<32>(s2);
ip.s3 = bitset<32>(s3);
ip.s4 = bitset<32>(s4);
ip.bin = ((ip.s1<<8 | ip.s2)<<8 | ip.s3)<<8 | ip.s4;
ips.push_back(ip);
bool flag =false;
for(auto j:idx){
ip_addr tmp = ips[j];
unsigned int tmp_l = tmp.l;
bool current_wider = tmp.l>ip.l;
unsigned int len = current_wider?ip.l:tmp.l;
string compare="00000000000000000000000000000000";
string _xor = (tmp.bin^ip.bin).to_string();
if(compare.substr(0,len) == _xor.substr(0,len)){
if(current_wider){
idx.erase(j);
}
else{
flag = true;
}
}
}
if(flag){
continue;
}
idx.insert(i);
}
cout<<idx.size()<<endl;
for(auto j:idx){
ip_addr ip = ips[j];
cout<<ip.s1.to_ulong()<<'.'<<ip.s2.to_ulong()<<'.'<<ip.s3.to_ulong()<<'.'<<ip.s4.to_ulong()<<'/'<<ip.l<<endl;
}
return 0;
}
查看原帖
点赞 4
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛油的搬砖plog #
16420次浏览 85人参与
# 哪些企业的面试体验感最差? #
22936次浏览 224人参与
# 一人一个landing小技巧 #
15703次浏览 296人参与
# 我在牛爱网找对象 #
174812次浏览 1278人参与
# 面试问题记录 #
102495次浏览 1433人参与
# 腾讯音乐求职进展汇总 #
84951次浏览 471人参与
# 2022毕业生求职现身说法 #
82360次浏览 686人参与
# 520告白墙 #
7653次浏览 143人参与
# 找工作的破防时刻 #
17252次浏览 273人参与
# 实习学不到东西怎么办? #
200512次浏览 2082人参与
# 你觉得第一学历对求职有影响吗? #
74709次浏览 545人参与
# 职场吐槽大会 #
195571次浏览 1578人参与
# 微软计划裁员 #
2177次浏览 56人参与
# 贝壳求职进展汇总 #
20417次浏览 144人参与
# 选完offer后,你后悔学本专业吗 #
42525次浏览 221人参与
# 安利/避雷我的专业 #
70871次浏览 502人参与
# 实习生应该准时下班吗 #
210616次浏览 1358人参与
# 工作中,你有没有遇到非常爱骂人的领导? #
26823次浏览 154人参与
# 华子oc时间线 #
1181739次浏览 6257人参与
# 通信硬件知识分享 #
25219次浏览 457人参与