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. Django框架的forms组件与一些补充

    目录 一.多对多的三种创建方式 1. 全自动 2. 纯手撸(了解) 3. 半自动(强烈推荐) 二.forms组件 1. 如何使用forms组件 2. 使用forms组件校验数据 3. 使用forms组 ...

  2. nacos配置中心之服务器端

    配置信息的发布 配置信息发布请求URL: POST: /v1/cs/configs nacos在STANDALONE模式或集群模式没有指定用mysql情况下使用derby数据库,在集群模式且指定mys ...

  3. 如何使用 Github Actions 自动抓取每日必应壁纸?

    如何白嫖 Github 服务器自动抓取必应搜索的每日壁纸呢? 如果你访问过必应搜索网站,那么你一定会被搜索页面的壁纸吸引,必应搜索的壁纸每日不同,自动更换,十分精美.这篇文章会介绍如何一步步分析出必应 ...

  4. CCF(消息传递口:80分):字符串相关+队列

    消息传递口 201903-4 本题主要是利用队列进行模拟,因为一开始我没有注意到要按照顺序,所以一开始的解法错误. #include<iostream> #include<algor ...

  5. 《进击吧!Blazor!》系列入门教程 第一章 6.安全

    <进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门教程视频,此教程能让一个从未接触过Blazor的程序员掌握开发Blazor应用的能力. 视频地址:https://s ...

  6. 用水浒传来学习OKR

    用水浒传来学习OKR 目录 用水浒传来学习OKR 0x00 摘要 0x01 OKR 1.1 基本概念 1.2 OKR管理的意义 1.3 Objective 1.3.1 什么是好的O 1.3.2 上下级 ...

  7. Django之cookie 与session组件

    一.会话跟踪技术 1.1 什么是会话跟踪 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而 ...

  8. .NET初探源代码生成(Source Generators)

    前言 Source Generators顾名思义代码生成器,可进行创建编译时代码,也就是所谓的编译时元编程,这可让一些运行时映射的代码改为编译时,同样也加快了速度,我们可避免那种昂贵的开销,这是有价值 ...

  9. SIP (Session Initiation Protocol) 协议

    Session Initiation Protocol 介绍 SIP是VoIP技术最常使用的协议,它是一种应用程序层协议,可与其他应用程序层协议配合使用,以控制Internet上的多媒体通信会话. V ...

  10. Linux 软链接link/ln -s

    在Linux中,链接分为软的和硬的,至于两者之间有什么差别,大家可以参考下https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-li ...