ccf 2018-12-3 CIDR合并

这是第一次参加ccf考试,当时考场上只得了50分,思路有点不清晰,回来整理了一下思路

其实考试题目都给了思路了

我的思路就是一个结构体,记录IP地址四段中的每一段的大小,还有一个len记录IP地址的长度

排序操作使用sort函数自动

从小到大合并使用另一个list来存储

统计合并就像进栈出栈一样

其实这两个我都想用list直接删除的好,但并不会这种操作

下面是满分代码,附带了两个自己的测试用例

#include<iostream>
#include<string>
#include<list>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
struct IP{
	string ip;
	int a[4]={0,0,0,0};
	int len;
	friend bool operator <(const IP &ip1,const IP &ip2){//用于排序,以IP值为第一关键字,长度为第二关键字 
		if(ip1.a[0]<ip2.a[0])
			return true;
		else if(ip1.a[0]==ip2.a[0]){
			if(ip1.a[1]<ip2.a[1])
				return true;
			else if(ip1.a[1]==ip2.a[1]){
				if(ip1.a[2]<ip2.a[2])
					return true;
				else if(ip1.a[2]==ip2.a[2]){
					if(ip1.a[3]<ip2.a[3])
					    return true;
					else if(ip1.a[3]==ip2.a[3]){
						if(ip1.len<ip2.len)
							return true;
					}
				}
			}
		}
		return false;
	}
};
int sti(string s){//字符串转数字 
	int a;
	char b[20];
	for(int i=0;i<s.length();i++)
		b[i]=s[i];
	sscanf(b,"%d",&a);
	return a;
}
IP toStandard(string str){//标准化 
	int len=0,l1=0,ui=0;
	IP iip;
	int f=str.find('/');
	string su=str;
	if(f!=string::npos){
		string h=str.substr(f+1,str.length()-f-1);
		len=sti(h);
		su=str.substr(0,f);
	} 
	su+=".";
	f=su.find('.');
	while(f!=string::npos){
		iip.a[ui]=sti(su.substr(0,f));
		su=su.substr(f+1,su.length()-f-1);
		ui+=1;
		l1+=8;
		f=su.find('.');
	}
	if(!len) iip.len=l1;
	else iip.len=len;
	return iip;
}
int pow_2(int i){//2的i次方 
	int s=1;
	while(i--){
		s*=2;
	}
	return s;
}
int contain(int &x,int y){
//二进制中是否包含某一位,比如255包含128,即11111111,那么x=255,y=128,就是最高位为1,如果包含,就减去128 
	if(x-y>=0){
		x-=y;
		return 1;
	}
	return -1;
}
bool subset(const IP x,const IP y){//x是否包含y 
	if(x.len>y.len) return false;
	else{
		int x1=x.len,y1=y.len,s=0;
		while(x1>=8){
			if(x.a[s]!=y.a[s])
				return false;
			x1-=8;
			s+=1;
		}
		int xx=x.a[s],yy=y.a[s];
		for(int i=0;i<x1;i++){
			if((contain(xx,pow_2(7-i))*contain(yy,pow_2(7-i)))==1)
				continue;
			else return false;
		}
		return true;	
	}
}
bool sum(const IP x,const IP y){//x,y是否互补 
	if(x.len!=y.len) return false;
	else{
		int x1=x.len,s=0;
		while(x1>8){
			if(x.a[s]!=y.a[s])
				return false;
			x1-=8;
			s+=1;
		}
		int xx=x.a[s],yy=y.a[s];
		for(int i=0;i<x1-1;i++){
			if((contain(xx,pow_2(7-i))*contain(yy,pow_2(7-i)))==1)
				continue;
			else return false;
		}
		if((contain(xx,pow_2(8-x1))==-1&&contain(yy,pow_2(8-x1)))==1)
			return true;
		else return false;	
	}
}
int main(){
	int n;
	list<IP> lis,vec;
	cin>>n;
	getchar();
	while(n--){
		string line;
		getline(cin,line);
		IP ipp=toStandard(line);
		lis.push_back(ipp);
	}//输入并且标准化 
	lis.sort();
	vector<IP> vect;
	vec.push_back(lis.front());
	lis.pop_front();
	for(list<IP>::iterator it=lis.begin();it!=lis.end();it++){
		if(!subset(vec.back(),*it)) vec.push_back(*it);
		else continue;
	}//从小到大合并 
	vect.push_back(vec.front());
	vec.pop_front();
 	while(!vec.empty()){
 		if(sum(vect.back(),vec.front())){
 			vec.pop_front();
 			if(vect.size()==1){
 				vect.back().len-=1;
			 }
			else{
				vec.push_front(vect.back());
 				vec.front().len-=1;
 				vect.pop_back();	
			}
		 }
		else{
			vect.push_back(vec.front());
			vec.pop_front();
		}	
	}//同级合并 
	for(vector<IP>::iterator iter=vect.begin();iter!=vect.end();iter++){
		cout<<iter->a[0]<<"."<<iter->a[1]<<"."<<iter->a[2]<<"."<<iter->a[3]<<"/"<<iter->len<<endl;
	}
} 
/*
5
10.12.16/24
10.12.16.128/25
10.12.16.0/25
10.23.28.224/27
10.23.28.192/27

5
10.12.16.128/25
10.12.16.128/25
10.12.16.0/25
10.23.28.224/27
10.23.28.192/27
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
2018-CCF面向电信行业存量用户的智能套餐个性化匹配,是指基于用户的个性需求、消费行为和网络服务质量,利用智能算法和数据分析技术,为电信用户提供量身定制的套餐。 首先,个性化匹配可以根据用户的通信习惯和需求,提供不同的套餐选项。例如,对于经常拨打电话的用户,可以提供通话时长较长、拨打费用较低的套餐;对于经常使用流量的用户,可以提供流量较大、速度较快的套餐。通过根据用户的消费习惯和需求进行个性化匹配,可以增加用户对套餐的满意度,提高用户留存率。 其次,个性化匹配可以根据用户的消费行为和网络服务质量,提供优质的套餐。通过对用户的消费行为进行分析,可以预测用户的需求和偏好,从而为用户提供更符合他们实际需求的套餐。同时,还可以根据用户所处的网络环境和网络服务质量的数据,提供相应的套餐选择,以保证用户的网络体验。这样能够增强用户对电信运营商的信任感,提高用户的满意度和忠诚度。 最后,个性化匹配还可以通过智能算法和数据分析技术,进行动态调整和优化。通过对用户的数据进行实时监测和分析,可以发现用户的变化需求和偏好,从而及时调整套餐,并根据用户的反馈和评价进行优化。这样可以实现不断的个性化匹配,提供更适合用户需求的套餐,进一步提升用户的满意度和忠诚度。 总而言之,2018-CCF面向电信行业存量用户的智能套餐个性化匹配,通过根据用户个性需求、消费行为和网络服务质量,利用智能算法和数据分析技术,为用户量身定制套餐,提高用户满意度、留存率和忠诚度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值