2019牛客暑期多校训练营(第六场) Shorten IPv6 Address

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

You are given an IPv6 address which is a 128-bit binary string. Please determine its shortest representation according to the following rules:
  • Express the address in hexadecimal representation and use a colon ':' to split every four hex digits. Every four digits are called a field. For example, '0000:0000:0123:4567:89ab:0000:0000:0000'.
  • Leading zeros in a field can be omitted. For example, the above IPv6 address can be shortened to '0:0:123:4567:89ab:0:0:0'.
  • Consecutive zero fields (with colons near them) consisting of at least two fields can be replaced by a double colon '::'. Besides, no more than one double colon can be used in an address. For example, the above IPv6 address can be shortened to '0:0:123:4567:89ab::' or '::123:4567:89ab:0:0:0', but not '::123:4567:89ab::'.
  • If there are multiple shortest forms of the same length, use the lexicographically (regard the shorten IPv6 address as string) smallest one.
If the above rules conflict with rules in the real world, please refer to the rules mentioned in this problem.

输入描述:

There are multiple test cases. The first line contains an integer T (1≤T≤10001 \leq T \leq 10001T1000), indicating the number of test cases. Test cases are given in the following.
Each test case consists of only one line, containing a 128-bit binary string.

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer to this test case.

输入

3
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001101000101011001111000100110101011000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000010010001100000000000000000000000000000000000000000000000001000101011001111000100110101011

输出

Case #1: ::
Case #2: 0:0:123:4567:89ab::
Case #3: 0:0:123::4567:89ab

题解:模拟。连续0的个数首先应该取最长,并列时应取字典序最小。':'的ASCII为58

代码:
#include<bits/stdc++.h>
using namespace std;
string s;
int w[100];
void init();
void solve();
int main()
{
  int T,cnt=0;
  scanf("%d",&T);
  while(T--)
  {
    cin>>s;
    init();
    printf("Case #%d: ",++cnt);
    solve();
  }
  system("pause");
  return 0;
}
void init()
{
  int i,j;
  for(i=1;i<=8;i++)
   {
     w[i]=0;
     for(j=(i-1)*16;j<16*i;j++)
      w[i]=w[i]*2+(s[j]-'0');
   }
}
void solve()
{
  int i,j,mmax=-1,index;
  for(i=1;i<=8;i++)
  {
    if(w[i]) continue;
    for(j=i;j<=8;j++)
    {
      if(w[j]) break;
      if(j-i+1>mmax||(j-i+1==mmax&&(index==1||j!=8))) //核心代码
        mmax=j-i+1,index=i;
    }
  }
  if(mmax<2) for(i=1;i<=8;i++) printf("%x%c",w[i],i!=8?':':'\n');
  else
   {
     for(i=1;i<index;i++) printf("%x:",w[i]);
     if(index==1) printf(":");
     printf(":");
     if(index+mmax>8) printf("\n");
     for(i=index+mmax;i<=8;i++) printf("%x%c",w[i],i!=8?':':'\n');
   }
}

转载于:https://www.cnblogs.com/VividBinGo/p/11312687.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值