题目链接:https://ac.nowcoder.com/acm/contest/886/B

题目大意

  给定一个 128 位的二进制 ip 地址,让你以 16 位一组,每组转成 16 进制,用冒号连接,并且可以选择一次且仅一次把 ip 数组中值连续为 0 的组删去(详情看样例),求如此操作后最短且字典序最小的 ip 字符串。

分析

  bitset + sprintf。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef vector< LL > VLL;
typedef vector< VLL > VVLL;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
LL mod = 1e9 + ;
const int maxN = 1e6 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int T;
bitset< > ip[];
string IP, ans; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
scanf("%d", &T);
For(cases, , T) {
IP = ":";
Rep(i, ) {
cin >> ip[i];
char buf[]; sprintf(buf, "%x", ip[i].to_ulong());
IP += string(buf) + ":";
} string ans = IP.substr(, IP.size() - );
rFor(i, IP.size() - , ) {
if(IP[i] == '') { // 记录连续 0 的数量并更新答案
int cnt = , j = i - ; while(j >= && IP[j] == ':' && IP[j + ] == '') {
j -= ;
++cnt;
} if(cnt >= ) {
j += ;
string ret = IP.substr(, j) + IP.substr(i + ); if(ret[] != ':') ret = ret.substr();
if(ret[ret.size() - ] != ':') ret = ret.substr(, ret.size() - ); if(ans.size() > ret.size()) ans = ret;
else if(ans.size() == ret.size()) ans = min(ans, ret);
i = j;
}
}
} cout << "Case #" << cases << ": " << ans << endl;
}
return ;
}

最新文章

  1. 关于python数据序列化的那些坑
  2. JavaScript数字精度丢失问题总结
  3. AOJ DSL_2_D Range Update Query (RUQ)
  4. MIT 6.828 JOS学习笔记6. Appendix 1: 实模式(real mode)与保护模式(protected mode)
  5. mac os 添加用户到组 命令
  6. log4net:保存日志到数据库
  7. jquery.hichartTable.js插件绘图
  8. RSA 加解密
  9. GWT 中日期格式化 ,处置Date
  10. 【EJS】
  11. CTF---Web入门第八题 Guess Next Session
  12. [PA2014]Druzyny
  13. css长度单位学习(em,rem,px,vw,vh)
  14. Springmvc架构
  15. docker启动容器报错: could not synchronise with container process: not a directory
  16. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Max, Min, Count , DistinctCount 以及其它 TopCount, Generate
  17. ElasticSearch实战——.Net Core中的应用
  18. 解决Linux里面未启用网卡的问题
  19. 微信小程序性能测试之jmeter踩坑秘籍(前言)
  20. Javascript-蔬菜运算价格

热门文章

  1. 构造——cf1202d
  2. elementUI表格行的点击事件,点击表格,拿到当前行的数据
  3. HBase自定义MapReduce
  4. (转)ubuntu 下安装mysql5.5.30的过程以及遇到的问题
  5. sklearn中standardscaler中fit_transform()和transform()有什么区别,应该怎么使用?
  6. 2、Android自动测试之Monkey工具
  7. 1、Appium Desktop介绍
  8. cmd 修改当前路径
  9. HDU 6651 Final Exam (思维)
  10. JAVA FileUtils(文件读写以及操作工具类)