当前位置:网站首页>1038 Recover the Smallest Number
1038 Recover the Smallest Number
2022-07-03 00:57:00 【Brosto_ Cloud】
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.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤104) 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.
Output Specification:
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287Custom sort function :
When two strings are not prefixed by one and the other , Sort directly in ascending order ;
When one is the prefix of another : Take the first character after the prefix of a long string ( That is, when the short string length is length when , Take the long string s[length]) Compare it with the first character of the short string , Put the smaller one in front ;
This ensures that the order between each two strings must be the smaller of the two permutations .
After sorting, the answer string is formed in a new order , Take care to remove leading zeros , Note that the answer is 0 The situation of .
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string a[10010];
bool cmp(string s1, string s2) {
int length = min(s1.size(), s2.size());
for (int i = 0; i < length; i++) {
if (s1[i] != s2[i]) {
return s1 < s2;
}
}
if (s1.size() < s2.size()) {
if (s2[length] > s1[0]) {
return true;
} else {
return false;
}
} else {
if (s1[length] > s2[0]) {
return false;
} else {
return true;
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n, cmp);
bool flag = 0;
string ans = "";
for (int i = 0; i < n; i++) {
ans += a[i];
}
for (int i = 0; i < ans.size(); i++) {
if (ans[i] != '0') {
flag = 1;
}
if (flag) {
cout << ans[i];
}
}
if (flag == 0) {
cout << 0;
}
return 0;
}边栏推荐
- 解决ReactNative使用webView存在缓存问题
- 2022上半年值得被看见的10条文案,每一句都能带给你力量!
- Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
- [overview of AUTOSAR three RTE]
- 全志A40i/T3如何通过SPI转CAN
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- 【AutoSAR 九 C/S原理架构】
- Vulkan is not a "panacea"“
- leetcode-224:基本计算器
- 数组与集合性能比较
猜你喜欢

Shell implements basic file operations (cutting, sorting, and de duplication)

Arduino开发之按键检测与正弦信号输出
![[AUTOSAR XIII NVM]](/img/38/805ab70f199e2cfad4d9dae0e2c1ff.png)
[AUTOSAR XIII NVM]

Vulkan practice first bullet
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

First hand evaluation of Reza electronics rz/g2l development board

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time

(C语言)数据的存储

【AutoSAR 十二 模式管理】
随机推荐
leetcode-2280:表示一个折线图的最少线段数
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
[Luogu p4320] road meets (round square tree)
[AUTOSAR XIII NVM]
【AutoSAR 十 IO架构】
About qbytearray storage hexadecimal and hexadecimal conversion
【AutoSAR 二 AppL概述】
线程的启动与优先级
【AutoSAR 十二 模式管理】
Leetcode-224: basic calculator
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Vulkan performance and refinement
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
[AUTOSAR 11 communication related mechanism]
leetcode-2115:从给定原材料中找到所有可以做出的菜
Linux软件:如何安装Redis服务
Vulkan并非“灵药“
[AUTOSAR + IO Architecture]
tail -f 、tail -F、tailf的区别