这是第一次参加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
*/