当前位置:网站首页>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;
}边栏推荐
- File operation io-part2
- Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
- Attributeerror: 'tuple' object has no attribute 'layer' problem solving
- MySQL multi table joint deletion
- 解决ReactNative使用webView存在缓存问题
- Array and collection performance comparison
- 深度剖析数据在内存中的存储
- [AUTOSAR II appl overview]
- [AUTOSAR eight OS]
- Leetcode-1964: find the longest effective obstacle race route to each position
猜你喜欢

测试右移:线上质量监控 ELK 实战

瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南

AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决

Illustrated network: what is virtual router redundancy protocol VRRP?

Is there a free text to speech tool to help recommend?

Vulkan practice first bullet

【AutoSAR 七 工具链简介】

使用jenkins之二Job

The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
随机推荐
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Shell implements basic file operations (SED edit, awk match)
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
[AUTOSAR VI description document]
【AutoSAR 四 BSW概述】
Linux Software: how to install redis service
Rust字符串切片、结构体和枚举类
百度智能云牵头打造智能云综合标准化平台
[AUTOSAR XIII NVM]
指针进阶(一)
瑞萨电子RZ/G2L开发板上手评测
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
数据分析思维分析犯法和业务知识——分析方法(一)
Leetcode-241: designing priorities for operational expressions
Advanced pointer (I)
KingbaseES ALTER TABLE 中 USING 子句的用法
[AUTOSAR 11 communication related mechanism]
Infrared thermography temperature detection system based on arm rk3568
Leetcode-934: the shortest Bridge
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议