博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CIDR合并
阅读量:4512 次
发布时间:2019-06-08

本文共 2415 字,大约阅读时间需要 8 分钟。

code

#include 
#include
#include
#include
#include
#include
using namespace std;struct IP{ string ip; int len=0; friend bool operator<(const IP &i,const IP &j) { long long i1=stoll(i.ip.substr(0,i.ip.find('/'))); long long j1=stoll(j.ip.substr(0,j.ip.find('/'))); if(i1!=j1) return i1<=j1; else return i.len<=j.len; }};int str_to_int(const string &str){ int num; stringstream ss; ss<
>num; return num;}void format(IP &ip){ int f=ip.ip.find('/'); string sub; int count_dot=0; if(f!=string::npos) { //先暂存长度 sub=ip.ip.substr(f+1,ip.ip.length()-f-1); ip.len=str_to_int(sub); //统计点的个数 int count_dot=0; sub=ip.ip; while((f=sub.find('.'))!=string::npos) { sub=sub.substr(f+1,sub.length()-f-1); ++count_dot; } //补全ip if(count_dot!=3) { ip.ip.erase(ip.ip.find('/'),ip.ip.length()-ip.ip.find('/')); for(int i=0;i<3-count_dot;++i) ip.ip.append(".0"); ip.ip.append("/").append(to_string(ip.len)); } } else { sub=ip.ip; while((f=sub.find('.'))!=string::npos) { sub=sub.substr(f+1,sub.length()-f-1); ++count_dot; } if(count_dot!=3) for(int i=0;i<3-count_dot;++i) ip.ip.append(".0"); ip.ip.append("/").append(to_string((count_dot+1)*8)); } //存储标准型的ip中的/len string s=ip.ip.substr(ip.ip.find("/"),ip.ip.length()-ip.ip.find("/")); //存储ip string s1=ip.ip.substr(0,ip.ip.find('/')); ip.ip.clear(); char *s2=new char[s1.size()];//要为s2申请空间后才可赋值 strcpy(s2,s1.c_str()); vector
v; //取出以'.'分割的数 char *p=strtok(s2,"."); while(p) { v.push_back(str_to_int(p)); p=strtok(NULL,"."); } //变成2进制 string s3; for(int i=0;i
i.ip.length()||i.len>j.ip.length()) return false; return i.ip.compare(0,i.len,j.ip,0,i.len)==0?true:false;}int main(){ int n; cin>>n; IP ip; list
l; //1.格式化输入;把所有输入的类型都变成标准型,ip用二进制表示 while(n--) { cin>>ip.ip; format(ip); l.push_back(ip); } l.sort(); //2.从小大大合并 for(auto i=l.begin();i!=l.end();) { list
::iterator next=i; ++next; if(next==l.end()) break; if(check(*i,*next)) l.erase(next); else ++i; } //3.合并同级 for(auto i=l.begin();i!=l.end();) { list
::iterator next=i; ++next; if(next==l.end()) break; IP ip1=*i; IP ip2=*next; if(ip1.len==ip2.len&&ip1.len>0&&ip1.ip[ip1.len-1]=='0') { IP tmp=ip1; --(tmp.len); if(check(tmp,ip2)) { l.erase(next); *i=tmp; if(i!=l.begin()) --i; } else ++next; } else ++i; } for(auto i=l.begin();i!=l.end();++i) cout<<(*i).ip<

 

转载于:https://www.cnblogs.com/tianzeng/p/10500955.html

你可能感兴趣的文章
Discretized Streams, 离散化的流数据处理
查看>>
Spark源码分析 – SchedulerBackend
查看>>
黑马程序员 Java输入\输出
查看>>
python字符串处理
查看>>
live555学习笔记4-计划任务(TaskScheduler)深入探讨
查看>>
【Unity3D】获取鼠标在三维空间(世界坐标系)的位置
查看>>
Python虚拟机函数机制之名字空间(二)
查看>>
线段树
查看>>
SharePoint2010联合搜索——Google、百度
查看>>
php静态
查看>>
python基础之文件操作
查看>>
在eclipse里头用checkstyle检查项目出现 File contains tab characters (this is the first instance)原因...
查看>>
个人github链接及git学习心得总结
查看>>
c++ 计算器 带括号 代码实现
查看>>
objective -c初写
查看>>
C#中如何设置窗体的默认按钮和取消按钮
查看>>
[Swift]LeetCode276. 粉刷栅栏 $ Paint Fence
查看>>
[Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns
查看>>
break语句和continue语句
查看>>
java代码中添加log4j日志
查看>>