思路
- 没啥可说的,根据题目要求写就好
- 由于比较菜又很懒,所以超时了,就懒得再继续做了
- 这次给我最大的体验就是好好看测试样例,一开始一直0分,看了测试样例才知道感情我排序那一步都没对,才改了cmp函数。
代码(90分超时)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct ip{
int four[4];
int len;
};
bool cmp(ip ad1,ip ad2){
for(int i=0;i<4;i++){
if(ad1.four[i]!=ad2.four[i])
return ad1.four[i]<ad2.four[i];
}
return ad1.len<ad2.len;
}
int compare(ip ad1,ip ad2){
int min=ad1.len;
int first=min/8;
int second=min%8;
int flag=1;
for(int j=0;j<first;j++){
if(ad1.four[j]!=ad2.four[j]){
flag=0;
break;
}
}
if(flag==1){
int small=ad1.four[first];
int big=ad2.four[first];
int top=128;
for(int j=0;j<second;j++){
if(small/top!=big/top){
flag=0;
break;
}
small%=top;
big%=top;
top/=2;
}
}
return flag;
}
int main(){
ios::sync_with_stdio();
int n;
cin>>n;
vector<ip> save;
for(int i=0;i<n;i++){
string temp;
cin>>temp;
int nowip=0;
int coupoint=0;
ip nowadd;
int flag=0;
for(int j=0;j<temp.size();j++){
if(temp[j]>='0' && temp[j]<='9'){
nowip=nowip*10+temp[j]-'0';
}
else if(temp[j]=='.'){
nowadd.four[coupoint]=nowip;
nowip=0;
coupoint++;
}
else if(temp[j]=='/'){
flag=1;
nowadd.four[coupoint]=nowip;
coupoint++;
int nowlength=0;
j++;
while(j<temp.size() && temp[j]>='0' && temp[j]<='9'){
nowlength=nowlength*10+temp[j]-'0';
j++;
}
nowadd.len=nowlength;
}
}
if(flag==0){
nowadd.four[coupoint]=nowip;
coupoint++;
nowadd.len=8*coupoint;
}
if(coupoint<4){
while(coupoint<4){
nowadd.four[coupoint]=0;
coupoint++;
}
}
save.push_back(nowadd);
}
sort(save.begin(),save.end(),cmp);
for(int i=1;i<save.size();i++){
if(i<1)
continue;
int flag=compare(save[i-1],save[i]);
if(flag==1){
save.erase(save.begin()+i);
i--;
}
}
sort(save.begin(),save.end(),cmp);
for(int i=1;i<save.size();i++){
if(i<1)
continue;
if(save[i-1].len==save[i].len){
ip adda;
int k=4;
while(k--){
adda.four[k]=save[i-1].four[k];
}
adda.len=save[i-1].len-1;
if(compare(adda,save[i-1]) && compare(adda,save[i])){
save[i-1]=adda;
save.erase(save.begin()+i);
i--;
i--;
}
}
}
for(int i=0;i<save.size();i++){
cout<<save[i].four[0]<<"."<<save[i].four[1]<<"."<<save[i].four[2]<<"."<<save[i].four[3]<<"/"<<save[i].len<<endl;
}
return 0;
}