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. ...
随机推荐
- EF code first Mysql 更换主键类型
把主键从long 改为int 居然要删掉重建,不知道有没有更好的办法. 删除带外键的表: Add-Migration name 修改生成的文件,主要把dbo.删除 然后update-database
- 第四周四则运算3 PSP表格
PSP2.1 Personal Software Process Stages time Plan 计划 -Estimate 整数四则运算 分数四则运算 括号 括号四则运算 在主函数中调用 30m ...
- 2019-02-25 EST 科技文翻译
The Definition of Theme and Rheme The point of departure is equally presented to the speaker and to ...
- 六、在U-boot中让LCD填充纯色
1. 编译U-boot 准备好U-boot压缩包urbetter-u-boot-1.1.6-v1.0.tgz,输入命令:tar -xvf urbetter-u-boot-1.1.6-v1.0.tgz ...
- 莫烦tensorflow(3)-Variable
import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.ad ...
- L2-002. 链表去重(数组模拟)
L2-002. 链表去重 因为数值比较小,所以直接用数组来模拟 #include<cstdio> #include<cstring> #include<iostream& ...
- [转] [Elasticsearch] 数据建模 - 处理关联关系(1)
[Elasticsearch] 数据建模 - 处理关联关系(1) 标签: 建模elasticsearch搜索搜索引擎 2015-08-16 23:55 6958人阅读 评论(0) 收藏 举报 分类: ...
- file类和io流
一.file类 file类是一个可以用其对象表示目录或文件的一个Java.io包中的类 import java.io.File; import java.io.IOException; public ...
- str 类型
1.capitalize():首字母大写 2.center(size,fillwith): 3.count(sub,start,end):计算子序列的个数 4.decode() 5.encode() ...
- ECONOMETRICS CHAPTER3
前几天看王垠的一篇文章谈数学与编程,说数学公式之所以难以理解是因为缺乏统一的形式,而且经常有未定义的变量出现.看这本书时也正有此感,所以有时不必要拘泥公式是否前后一致,理解意思要紧. ♣普通最小二乘法 ...