原题链接
#include <iostream>
using namespace std;
#include <string>
#include <cstring>
#include <algorithm>
#include <list>
#define N 100005
int n;
struct Node{
string ip;
int len= 0 ;
Node ( ) {
}
int to_Int ( )
{
int ans= 0 ;
for ( int i= 0 ; i< 32 ; i++ )
{
ans* = 2 ;
ans+ = ip[ i] - '0' ;
}
return ans;
}
void print ( )
{
int ans[ 4 ] ;
memset ( ans, 0 , sizeof ( ans) ) ;
int cnt= 0 ;
for ( int i= 0 ; i< 32 ; i++ )
{
ans[ cnt] * = 2 ;
ans[ cnt] + = ip[ i] - '0' ;
if ( ( i+ 1 ) % 8 == 0 )
cnt++ ;
}
for ( int i= 0 ; i< 4 ; i++ )
{
if ( i== 0 )
;
else cout<< '.' ;
cout<< ans[ i] ;
}
cout<< '/' << len;
}
int match ( Node nn)
{
string aa= ip. substr ( 0 , len) ;
string bb= nn. ip. substr ( 0 , len) ;
if ( aa!= bb)
return 0 ;
if ( nn. len>= len)
return 1 ;
else return 0 ;
}
int together ( Node nn)
{
string aa= ip. substr ( 0 , len- 1 ) ;
string bb= nn. ip. substr ( 0 , len- 1 ) ;
if ( aa!= bb)
return 0 ;
if ( len!= nn. len)
return 0 ;
if ( ( ip[ len- 1 ] - '0' ) + ( nn. ip[ len- 1 ] - '0' ) == 1 )
return 1 ;
else return 0 ;
}
Node ( string str)
{
int cnt= 0 ;
int is_len= 0 ;
int num[ 4 ] ;
memset ( num, 0 , sizeof ( num) ) ;
for ( int i= 0 ; i< str. size ( ) ; i++ )
{
if ( str[ i] == '.' )
{
cnt++ ;
continue ;
}
if ( is_len== 1 )
{
len* = 10 ;
len+ = str[ i] - '0' ;
continue ;
}
if ( str[ i] == '/' )
{
is_len= 1 ;
continue ;
}
num[ cnt] * = 10 ;
num[ cnt] + = ( str[ i] - '0' ) ;
}
if ( ! is_len)
{
len= ( cnt+ 1 ) * 8 ;
}
for ( int i= 0 ; i< 4 ; i++ )
{
int tt= num[ i] ;
for ( int k= 7 ; k>= 0 ; k-- )
{
if ( ( tt>> k) & 1 )
{
ip+ = '1' ;
} else ip+ = '0' ;
}
}
}
bool operator < ( const Node & nn) const {
if ( ip!= nn. ip)
return ip< nn. ip;
return len< nn. len;
}
} ;
list< Node> ll;
int main ( )
{
cin>> n;
string str;
getchar ( ) ;
for ( int i= 0 ; i< n; i++ )
{
getline ( cin, str) ;
ll. push_back ( Node ( str) ) ;
}
ll. sort ( [ ] ( const Node & aa, const Node & bb) {
if ( aa. ip== bb. ip)
return aa. len< bb. len;
return aa. ip< bb. ip;
} ) ;
for ( auto tt= ll. begin ( ) ; tt!= ll. end ( ) ; tt++ )
{
auto next= tt;
next++ ;
while ( next!= ll. end ( ) && ( * tt) . match ( * next) )
{
ll. erase ( next) ;
next= tt;
next++ ;
}
}
for ( auto tt= ll. begin ( ) ; tt!= ll. end ( ) ; )
{
auto next= tt;
next++ ;
if ( next!= ll. end ( ) && ( * tt) . len- 1 >= 0 && ( * tt) . together ( * next) )
{
( * tt) . len- = 1 ;
ll. erase ( next) ;
if ( tt== ll. begin ( ) )
tt++ ;
else tt-- ;
continue ;
}
tt++ ;
}
for ( auto tt: ll)
{
tt. print ( ) ;
printf ( "\n" ) ;
}
return 0 ;
}