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 (≤) 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:

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的话需要进行特殊判断。不然的话会卡第三组数据。

1038 Recover the Smallest Number的更多相关文章

  1. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  2. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  3. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  4. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  5. pat 甲级 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)

    1038 Recover the Smallest Number (30 分)   Given a collection of number segments, you are supposed to ...

  7. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

  8. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

  9. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

  10. 1038 Recover the Smallest Number (30)(30 分)

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

随机推荐

  1. AOP面试造火箭始末

    本文已整理致我的github地址,欢迎大家 star 支持一下 这是一个困扰我司由来已久的难题,Dubbo 了解过吧,对外提供的服务可能有多个方法,一般我们为了不给调用方埋坑,会在每个方法里把所有异常 ...

  2. XAPKInstaller - XAPK游戏包安装器

    XAPKInstaller 一个用于安装XAPK游戏包的安装器. 程序需要读写存储与获取已安装应用权限才可正常运行. 长按条目可显示文件的详细信息. SDK小于24(Android N)的设备会显示应 ...

  3. Mybatis系列全解(六):Mybatis最硬核的API你知道几个?

    封面:洛小汐 作者:潘潘 2020 年的大疫情,把世界撕成几片. 时至今日,依旧人心惶惶. 很庆幸,身处这安稳国, 兼得一份安稳工. · 东家常讲的一个词:深秋心态 . 大势时,不跟风.起哄, 萧条时 ...

  4. Java 多线程 02

    多线程·线程间通信 和 GUI 单例设计模式 * A:单例设计模式 * 保证类在内存中只有一个对象 * B:如何保证 * a:控制类的创建,不让其他类来创建泵类的对象,私有化构造方法 * b:在本类中 ...

  5. CVE-2019-10758-Mongo-express-远程代码执行

    漏洞分析 https://xz.aliyun.com/t/7056 漏洞简介 mongo-express是一款mongodb的第三方Web界面,使用node和express开发. 如果攻击者可以成功登 ...

  6. golang——net/rpc/jsonrpc包学习

    1.jsonrpc包 该实现了JSON-RPC的ClientCodec和ServerCodec接口,可用于rpc包. 可用于跨语言使用go rpc服务. 2.常用方法 (1)func Dial(net ...

  7. python爬取考研专业信息

    伴随着2021考研成绩的公布,2021考研国家线也即将到来.大家是否有过考研的想法了?如果想考研我们就需要了解很多的信息,但是百度的上有太多信息需要我们去一一的鉴别,是比较浪费时间的.所以我们可以学习 ...

  8. 攻防世界 reverse Replace

    Replace 湖湘杯2018 查壳upx,手动脱壳,修复IAT,去掉重定向便可以运行. ida查看,流程清晰.关键函数check_E51090. int __cdecl main(int argc, ...

  9. JAVA面试核心知识点(一):计算机网络

    一·计算机网络 1.1 网络基础知识 OSI 七层协议(制定标准使用的标准概念框架): 物理层(传递比特流0101)->数据链路层(将比特流转换为逻辑传输线路)->网络层(逻辑编址,分组传 ...

  10. 一个C++引用库的头文件预编译陷阱

    写在前面 老胡最近在工作中,有个场景需要使用一个第三方库,引用头文件,链接库,编译运行,一切都很正常,但是接下来就遇到了一个很诡异的问题,调用该库的中的一个对象方法为对象修改属性的时候,会影响到对象的 ...