[General idea of the title] ____________
Give you n 128-bit binary IP addresses, output the shortest compressed IP address, if the length is the same, output the least dictionary order.
Compression rule: Every 16-bit binary number is a field, such as 0000000110001111, and then four-bit binary number is converted to a 16-bit number, such as 0000000110001111 to 01af, where the leading 0 of each field is removed, and if the leading value of this field is 0, it is denoted by zero. Then, if the values of two or more fields are zero in succession, they are denoted by two colons ":" which means that ":" can only be used once at most in an ip address.
(Dazzling!!!!!!!!!!!!!!!!)
[Sample input]
8
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001101000101011001111000100110101011000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001100000000000000000000000000000000000000000000000001000101011001111000100110101011
11111111111111111111111111110000000000000000000000000000000000000000000000001111000000000000000000000000000000000000000000000000
00010001000100010001000100010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000
00010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001
00000001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001
00010001000100010001000100010001000100000001000100010001000100010001000100010001000100010001000100010001000100010001000100010001
[Sample output]
Case #1: ::
Case #2: 0:0:123:4567:89ab::
Case #3: 0:0:123::4567:89ab
Case #4: ffff:fff0:0:0:f::
Case #5: 1111:1110:0:0:1::
Case #6: 1111:1111:1111:1111:1111:1111:1111:1111
Case #7: 111:1111:1111:1111:1111:1111:1111:1111
Case #8: 1111:1111:1011:1111:1111:1111:1111:1111
[Code]
1 #include <bits/stdc++.h> 2 #define ll long long 3 #define pai pair<int,int> 4 using namespace std; 5 int bit[4]= {8,4,2,1};//8421 Code table 6 map<int,string>MAP; 7 bool comp(string &a,string &b) 8 { 9 int len1=a.length(),len2=b.length(); 10 if(len1==len2)return a<b; 11 return len1<len2; 12 } 13 void init() 14 { 15 MAP[0]="0"; 16 MAP[1]="1"; 17 MAP[2]="2"; 18 MAP[3]="3"; 19 MAP[4]="4"; 20 MAP[5]="5"; 21 MAP[6]="6"; 22 MAP[7]="7"; 23 MAP[8]="8"; 24 MAP[9]="9"; 25 MAP[10]="a"; 26 MAP[11]="b"; 27 MAP[12]="c"; 28 MAP[13]="d"; 29 MAP[14]="e"; 30 MAP[15]="f"; 31 } 32 int main() 33 { 34 // cout<<int(':')<<" "<<int('a')<<" "<<int('0')<<endl; 35 // string a="::ffff:ffff:0:0:0"; 36 // string b="0:0:0:ffff:ffff::"; 37 // string c="0:0:0:1111:1111::"; 38 // string d="::1111:1111:0:0:0"; 39 // if(a>b)cout<<"a>b"<<endl; 40 // else if(a==b)cout<<"a=b"<<endl; 41 // else cout<<"b>a"<<endl; 42 // if(c>d)cout<<"c>d"<<endl; 43 // else if(c==d)cout<<"c=d"<<endl; 44 // else cout<<"d>c"<<endl; 45 init(); 46 int t; 47 scanf("%d",&t); 48 int k=1; 49 while(k<=t) 50 { 51 queue<pai>que; 52 string ans[10]; 53 int l=0,r=0; 54 int maxx=0; 55 getchar(); 56 for(int i=1; i<=8; i++) 57 { 58 char add[20]; 59 scanf("%16s",add); 60 int now=0; 61 int num=0; 62 bool flag=false; 63 for(int j=0; j<16; j++) 64 { 65 now=now+bit[j%4]*(add[j]-'0'); 66 // cout<<j<<" "<<now<<endl; 67 if(now>0)flag=true; 68 if(j%4==3) 69 { 70 num=num*10+now; 71 if(flag) 72 ans[i]+=MAP[now]; 73 now=0; 74 } 75 } 76 if(num==0) 77 { 78 ans[i]="0"; 79 } 80 } 81 int bitnum=0; 82 for(int i=1; i<=8; i++) 83 { 84 if(ans[i].compare("0")==0) 85 { 86 bitnum++; 87 } 88 else bitnum=0; 89 if(bitnum>=2) 90 { 91 if(maxx<=bitnum) 92 { 93 l=i-bitnum+1; 94 r=i; 95 que.push(pai(l,r)); 96 maxx=bitnum; 97 } 98 } 99 } 100 printf("Case #%d: ",k); 101 if(que.size()==0) 102 { 103 for(int i=1; i<=8; i++) 104 { 105 cout<<ans[i]; 106 if(i<8)printf(":"); 107 } 108 printf("\n"); 109 } 110 else 111 { 112 string out[1000]; 113 int ans_num=0; 114 while(!que.empty()) 115 { 116 pai p=que.front(); 117 que.pop(); 118 int _l=p.first,_r=p.second; 119 for(int i=1; i<=8;) 120 { 121 if(i==_l) 122 { 123 out[ans_num]+="::"; 124 i=_r+1; 125 } 126 else 127 { 128 out[ans_num]+=ans[i]; 129 if(i<8&&i+1!=_l) 130 out[ans_num]+=":"; 131 i++; 132 } 133 } 134 ans_num++; 135 } 136 sort(out,out+ans_num,comp); 137 cout<<out[0]<<endl; 138 // cout<<out[1]<<endl; 139 // cout<<out[2]<<endl; 140 } 141 k++; 142 } 143 return 0; 144 } 145 /* 146 */ 147 /* 148 8 149 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 150 00000000000000000000000000000000000000010010001101000101011001111000100110101011000000000000000000000000000000000000000000000000 151 00000000000000000000000000000000000000010010001100000000000000000000000000000000000000000000000001000101011001111000100110101011 152 11111111111111111111111111110000000000000000000000000000000000000000000000001111000000000000000000000000000000000000000000000000 153 00010001000100010001000100010000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000 154 00010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001 155 00000001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001 156 00010001000100010001000100010001000100000001000100010001000100010001000100010001000100010001000100010001000100010001000100010001 157 */