1038 Recover the Smallest Number

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

1|0Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

2|0Output Specification:

For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

3|0Sample Input:

5 32 321 3214 0229 87
 

4|0Sample Output:

22932132143287

 

题意:

  给出一组数字,找出由这组数字组合形成的最小字符串。

思路:

  先将这些数字按照字符串排序,然后依次输出,如果当前字符串和下一个字符串的子串相等,则比较下一个字符串的第一个字符和第len(current string)个字符的大小,输出较小的那个。然后将这个字符标记为已经访问过。下次循环再找出没有访问过的最小的字符串。

Code:

1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() { 6 int n; 7 cin >> n; 8 vector<string> v(n + 1); 9 vector<bool> used(n, false); 10 for (int i = 0; i < n; ++i) cin >> v[i]; 11 sort(v.begin(), v.begin() + n); 12 v.push_back("999999999"); 13 string res; 14 int cnt = 0, i = 0, j; 15 while (cnt < n) { 16 j = i + 1; 17 while (used[j]) ++j; 18 int len = v[i].length(); 19 if (v[i] == v[j].substr(0, len)) { 20 if (v[j][0] < v[j][len]) { 21 res += v[i]; 22 used[i] = true; 23 while (used[i]) ++i; 24 } else { 25 res += v[j]; 26 used[j] = true; 27 } 28 } else { 29 res += v[i]; 30 used[i] = true; 31 while (used[i]) ++i; 32 } 33 cnt++; 34 } 35 int start = 0; 36 while (res[start] == '0') start++; 37 if (start == res.length()) cout << 0 << endl; 38 else cout << res.substr(start) << endl; 39 return 0; 40 }

 

注意:

  如果结果是0的话需要进行特殊判断。不然的话会卡第三组数据。

 


__EOF__

本文作者Veritas des Liberty
本文链接https://www.cnblogs.com/h-hkai/p/12876013.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Veritas_des_Liberty  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
阅读排行:
· 终于决定:把自己家的能源管理系统开源了!
· 外部H5唤起常用小程序链接规则整理
· C#实现 Winform 程序在系统托盘显示图标 & 开机自启动
· 了解 ASP.NET Core 中的中间件
· 详解:订单履约系统规划
历史上的今天:
2019-05-12 453. Minimum Moves to Equal Array Elements
2019-05-12 423. Reconstruct Original Digits from English
  1. 1 Relaxing Rain Drops Rain Sounds / BodyHI / Rain for Deep Sleep
  2. 2 Sleep Rain Sounds Rain Sounds / BodyHI / Rain for Deep Sleep
  3. 3 In The Fields Rain Sounds / BodyHI / Rain for Deep Sleep
  4. 4 Asmr Rain Water Rain Sounds / BodyHI / Rain for Deep Sleep
  5. 5 Rain Study Rain Sounds / BodyHI / Rain for Deep Sleep
  6. 6 Meditation Rain Sound Rain Sounds / BodyHI / Rain for Deep Sleep
  7. 7 Rain Hitting Tin Roof Rain Sounds / BodyHI / Rain for Deep Sleep
  8. 8 Rain Sounds For Sleep Rain Sounds / BodyHI / Rain for Deep Sleep
  9. 9 Yoga Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  10. 10 Relaxing Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  11. 11 Window Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  12. 12 Hard Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  13. 13 Reiki Healing Nature Rain Sounds / BodyHI / Rain for Deep Sleep
  14. 14 Rain For Sleeping Rain Sounds / BodyHI / Rain for Deep Sleep
  15. 15 Rain On Roof Rain Sounds / BodyHI / Rain for Deep Sleep
  16. 16 Calming Ambience Rain Sounds / BodyHI / Rain for Deep Sleep
  17. 17 Rain Sounds To Sleep Rain Sounds / BodyHI / Rain for Deep Sleep
  18. 18 Baby Sleep Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  19. 19 Cosy Car Roof Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  20. 20 Waterfall & Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  21. 21 Therapeutic Sounds Rain Sounds / BodyHI / Rain for Deep Sleep
  22. 22 Peaceful Rain Rain Sounds / Rain for Deep Sleep
  23. 23 Rain For Study & Concentration Rain Sounds / BodyHI / Rain for Deep Sleep
  24. 24 Light Rain Tapping Asmr Rain Sounds / BodyHI / Rain for Deep Sleep
  25. 25 Stress Relief Sounds Rain Sounds / BodyHI / Rain for Deep Sleep
  26. 26 Nature & Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  27. 27 Blissful Nature Rain Sounds / BodyHI / Rain for Deep Sleep
  28. 28 Soothing Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  29. 29 Calm Anxiety Sounds Rain Sounds / BodyHI / Rain for Deep Sleep
  30. 30 Peaceful Sounds Rain Sounds / BodyHI / Rain for Deep Sleep
  31. 31 Light Tent Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  32. 32 Birds And Rain Rain Sounds / BodyHI / Rain for Deep Sleep
  33. 33 Deep Sleep Rainforest Rain Sounds / BodyHI / Rain for Deep Sleep
  34. 34 Heavy Rain On Leaves Rain Sounds / BodyHI / Rain for Deep Sleep
  35. 35 Wind And Rain Rain Sounds / BodyHI / Rain for Deep Sleep
Sleep Rain Sounds - Rain Sounds / BodyHI / Rain for Deep Sleep
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

纯音乐,请欣赏

点击右上角即可分享
微信分享提示