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. 后端程序员之路 44、Redis结合protobuf

    protobuf序列化速度不错,在往Redis里存对象时,用protobuf序列化可以节省内存,省去写序列化反序列化代码的工作. google protocol buffer 与 redis 结合使用 ...

  2. 剑指 Offer 58 - I. 翻转单词顺序 + 双指针

    剑指 Offer 58 - I. 翻转单词顺序 Offer_58_1 题目描述 方法一:使用Split函数 package com.walegarrett.offer; /** * @Author W ...

  3. POJ-1847(SPFA+Vector和PriorityQueue优化的dijstra算法)

    Tram POJ-1847 这里其实没有必要使用SPFA算法,但是为了巩固知识,还是用了.也可以使用dijikstra算法. #include<iostream> #include< ...

  4. OpenCV计算机视觉学习(13)——图像特征点检测(Harris角点检测,sift算法)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 前言 ...

  5. 利用xslt与xml实现具体字段字母的大小写转换

    定义一个全局的变量 <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" ...

  6. Debezium实现多数据源迁移(一)

    背景: 某公司有三个数据库,分别为MySql.Oracle和PostgreSql.原有业务的数据都是来自于这三个DB,此处委托将原有的三个数据库整合成一个Mysql. 要求: 1.不影响原有系统的继续 ...

  7. vue 实现页面嵌套pdf之vue-pdf插件

    近期vue移动端项目中遇到了页面内,嵌套展示pdf内容.实现方法很多种,可以用iframe嵌套,但不利于引擎优化seo.所以在网上找到了vue-pdf这个插件,这个插件非常好用,其中封装的方法也很多, ...

  8. 真会C#微信小程序的习题数据JSON文件下载链接

    完全没有精力去维护了,所以小程序停掉,集中精力做一件事. 链接: https://pan.baidu.com/s/1xL45KxDzR5oEQM6nwBA5rw 提取码: qv6n

  9. Go 语言入门教程,共32讲,6小时(已完结)

    Go语言入门教程,共32讲,6小时(完结) 在B站:https://www.bilibili.com/video/BV1fD4y1m7TD/

  10. 13 条高效实用的 JavaScript 单行代码

    JavaScript可以实现很多令人惊奇的事! 从复杂的框架到处理API,有太多的东西可以学习. 甚至,仅用一行代码,它也能完成一些很棒的工作. 不信?那么请看这13条JavaScript单行代码,用 ...