1038 Recover the Smallest Number
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的更多相关文章
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1038 Recover the Smallest Number (30 分)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1038. Recover the Smallest Number (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT 1038 Recover the Smallest Number[dp][难]
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- PAT 甲级 1038 Recover the Smallest Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
随机推荐
- Svelte 极简入门
弹指之间即可完成. 注意:原文发表于 2017-8-7,随着框架不断演进,部分内容可能已不适用. Svelte 是一种新型框架. 以往我们要引入一个框架或者类库,可以通过在页面上放置 ...
- 力扣896. 单调数列-C语言实现-简单题
题目 传送门 文本 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j, ...
- 译文《全新首发JDK 16全部新特性》
封面:洛小汐 译者:潘潘 JDK 8 的新特性都还没摸透,JDK 16 的新特性就提着刀来了. 郑重申明: 第一次冒险翻译专业领域的文献,可想而知,效果特别糟糕.一般翻译文献特别是 技术专业领域 的内 ...
- Linux:使用systemd管理进程
Blog:博客园 个人 概述 systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能 ...
- [UNP] IO 复用
UNP Part-2: Chapter 6. I/O Multiplexing: The select and poll Functions 的读书笔记. 在 这篇博客 的最后,我们对文章中的服务器- ...
- Netty源码 reactor 模型
翻阅源码时,我们会发现netty中很多方法的调用都是通过线程池的方式进行异步的调用, 这种 eventLoop.execute 方式的调用,实际上便是reactor线程.对应项目中使用广泛的NioE ...
- Intellij IDEA maven设置tomcat
1 pom.xml配置插件 <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId&g ...
- Aibabelx-shop 大型微服务架构系列实战之技术选型
一.本项目涉及编程语言java,scala,python,涉及的技术如下: 1.微服务架构: springboot springcloud mybatisplus shiro 2.全文检索技术 sol ...
- Elasticsearch 单字符串多字段查询
前言 有些时候,我们搜索的时候,只会提供一个输入框,但是会查询相关的多个字段,典型的如Google搜索,我们该如何用 Elasticsearch 如何实现呢? 实例 从单字符串查询的实例说起 创建测试 ...
- JS逆向-抠代码的第四天【手把手学会抠代码】
今天是md5巩固项目,该项目比昨天的复杂一些,但方法思路是一样的. 今天的目标:https://www.webportal.top/ 打开网站,填入账号密码(密码项目以123456做测试).点击登录抓 ...