CIDR合并

code
#include <iostream>
#include <list>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
struct IP
{
string ip;
;
friend bool operator<(const IP &i,const IP &j)
{
,i.ip.find('/')));
,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<<str;
ss>>num;
return num;
}
void format(IP &ip)
{
int f=ip.ip.find('/');
string sub;
;
if(f!=string::npos)
{
//先暂存长度
sub=ip.ip.substr(f+,ip.ip.length()-f-);
ip.len=str_to_int(sub);
//统计点的个数
;
sub=ip.ip;
while((f=sub.find('.'))!=string::npos)
{
sub=sub.substr(f+,sub.length()-f-);
++count_dot;
}
//补全ip
)
{
ip.ip.erase(ip.ip.find('/'),ip.ip.length()-ip.ip.find('/'));
;i<-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+,sub.length()-f-);
++count_dot;
}
)
;i<-count_dot;++i)
ip.ip.append(".0");
ip.ip.append()*));
}
//存储标准型的ip中的/len
string s=ip.ip.substr(ip.ip.find("/"),ip.ip.length()-ip.ip.find("/"));
//存储ip
,ip.ip.find('/'));
ip.ip.clear();
char *s2=new char[s1.size()];//要为s2申请空间后才可赋值
strcpy(s2,s1.c_str());
vector<int> v;
//取出以'.'分割的数
char *p=strtok(s2,".");
while(p)
{
v.push_back(str_to_int(p));
p=strtok(NULL,".");
}
//变成2进制
string s3;
;i<v.size();++i)
{
int t=v.at(i);
while(t)
{
s3.append(to_string(t%));
t/=;
}
}
reverse(s3.begin(),s3.end());
ip.ip.append(s3).append(s);
delete []s2;
}
bool check(const IP &i,const IP &j)
{
if(i.len>i.ip.length()||i.len>j.ip.length())
return false;
,i.len,j.ip,,i.len)==?true:false;
}
int main()
{
int n;
cin>>n;
IP ip;
list<IP> 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<IP>::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<IP>::iterator next=i;
++next;
if(next==l.end())
break;
IP ip1=*i;
IP ip2=*next;
&&ip1.ip[ip1.len-]==')
{
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<<endl;
;
}
CIDR合并的更多相关文章
- CCF 201812-3 CIDR合并
CCF 201812-3 CIDR合并 //100分 93ms #include<stdio.h>//CCF上stdio.h比cstdio快!!! #include<string.h ...
- CCF-CSP题解 201812-3 CIDR合并
题目想求与给定前缀列表等价的包含IP前缀数目最少的前缀列表. 首先是怎么存储前缀列表.用一个long long存储IP地址,再存一个前缀长度,封装在一个结构体里\(<ipNum, len> ...
- CCF201812-3 CIDR合并
按题意模拟即可...主要CCF吞代码... #include<bits/stdc++.h> #define pb push_back #define mp make_pair #defin ...
- CCF-CSP认证 C++题解目录
持续更新中,记录刷题过程并分享一下小小的心得总结. 试题编号 试题名称 标签 202006-1 线性分类器 | 题解 线性规划 202006-2 稀疏向量| 题解 归并排序思想 202006-3 化学 ...
- CIDR
CIDR的介绍: CIDR(Classless Inter-Domain Routing,无类域间路由选择)它消除了传统的A类.B类和C类地址以及划分子网的概念,因而可以更加有效地分配IPv4的地址空 ...
- 无分类编址(CIDR)构成超网
CIDR(无分类域间路由选择) CIDR最主要有两个以下特点: 消除传统的A,B,C地址和划分子网的概念,更有效的分配IPv4的地址空间,CIDR使IP地址又回到无分类的两级编码.记法:IP地址::= ...
- 第5章 子网划分和CIDR
第5章 子网划分和CIDR 划分网络 根据A类.B类或C类网络ID来识别网段具有一些局限性,主要是在网络级别之下不能对地址空间进行任何逻辑细分 如果一个IP是一个A类网络.数据报到达网关,然后传输到9 ...
- 无分类编址(CIDR,Class Inter-Domain-Routing)
CIDR全称是无分类域间路由选择,英文全称是Classless Inter-Domain Routing,大家多称之为无分类编址 CIDR的特点 (1)CIDR消除了传统的A类.B类和C类地址以及划分 ...
- IP地址和CIDR
IP地址(IPV4) IPV4的地址是一个32位的二进制数,由网络ID和主机ID两部分组成,用来在网络中唯一的标识一台计算机.IP地址通常用四组3位的十进制数表示,中间用.分割,例如:192.168. ...
随机推荐
- let var区别
function varTest() { var x = 1; if (true) { var x = 2; // 同样的变量! console.log(x); } console.log(x); } ...
- LeetCode 43 字符串相乘
题目: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", ...
- ajax之同步异步详解
同步:一堆任务,按顺序去执行,只能一件件的来,如一个厨师做菜 异步:一堆任务,可以同时进行,如多个厨师做菜 ...
- ubuntu 更改hostname, hosts后没有办法执行sudo 问题
由于主机名有重复的情况,所有需要更改主机名,遇到几个问题,主机名更改,重启系统以后不能执行sudo,另外也不知道root用户名密码.这个时候陷入了死循环. 网上搜集的办法,都是用sudo 命令去更改, ...
- Ajax 以及 前端JSP页面如何查看数值
$.ajax({ url: ctx + "/unit/rsdl/qyjy/getDljgCode", type: "post", success: functi ...
- L313 珊瑚裸鼠灭绝
This week the Australian government declared the extinction of a tiny rodent called Bramble Cay melo ...
- JAVA线程sleep与wait区别
sleep就是正在执行的线程主动让出cpu,cpu去执行其他线程,在sleep指定的时间过后,cpu才会回到这个线程上继续往下执行,如果当前线程进入了同步锁,sleep方法并不会释放锁,即使当前线程使 ...
- MySQL:日期函数、时间函数处理(转)
date_add() 增加MYSQL 获取当前时间加上一个月 ; date_sub()减少 date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day ...
- 第二十六课 典型问题分析(Bugfix)
问题1: glibc中的strdup实现如下: 没有对参数s进行空指针判断. 我们的Exception.cpp中应做改进: 在第12行进行判断空指针操作. 问题2: t1在析构时会抛出异常,我们在re ...
- Failed to decode response: zlib_decode(): data error
/********************************************************************** * Failed to decode response: ...