1038 Recover the Smallest Number (30 分)

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 (≤10​4​​) 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

题意:

给定n个数,要求任意组合使得组合得到的数最小。

思路:

之前做过一道类似的。这道题给的样例好啊。

表面上看好像是应该将输入的数字存成字符,按照字典序从小到大输出。但是观察样例会发现并不是这样的。

因为单个的字典序最小并不代表组合了之后的字典序最小。

应该要写一个cmp, 用的是组合之后字典序最小的排前面

还有要注意都是0的时候要记得最后输出一个0

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e4 + ;
string strnum[maxn];
int n; bool cmp(string a, string b)
{
return a + b < b + a;
} int main()
{
scanf("%d", &n);
for(int i = ; i < n; i++){
cin>>strnum[i];
}
//while(cin>>strnum[n++]);
sort(strnum, strnum + n, cmp);
bool flag = true;
for(int i = ; i < n; i++){
int l = strnum[i].length();
for(int j = ; j < l; j++){
if(flag && strnum[i][j] == '')continue;
if(flag && strnum[i][j] != ''){
flag = false;
}
printf("%c", strnum[i][j]);
}
}
if(flag){
printf("");
}
printf("\n");
return ;
}

PAT甲1038 Recover the smallest number的更多相关文章

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

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

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

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

  3. PAT 甲级 1038 Recover the Smallest Number

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

  4. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

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

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

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

  6. 1038 Recover the Smallest Number (30 分)

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

  7. 1038. Recover the Smallest Number (30)

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

  8. PAT 1038 Recover the Smallest Number (30分) string巧排序

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

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

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

随机推荐

  1. Android系统自带样式(android:theme)

    Theme.Dialog : (图1)Activity显示为对话框模式 Theme.NoTitleBar : (图2)不显示应用程序标题栏 Theme.NoTitleBar.Fullscreen : ...

  2. Java使用ListIterator逆序ArrayList

    对于列表而言,除了Iterator,还提供了一个功能更加强大的ListIterator.它可以实现逆序遍历列表中的元素.本示例将使用其逆序遍历ArrayList. 思路分析:要逆序遍历某个列表,首先要 ...

  3. javascript 显示一定范围内的素数(质数)

    素数又称质数,是大于1的自然数,并且只有1和它本身两个因数. 具体实现代码如下: 运行代码 <!DOCTYPE HTML> <html> <head lang=" ...

  4. Import VMware ESXi from VirtualBox

    VirtualBox can export appliance VMs to OVF format. And you can import the ovf format to VMware ESXi, ...

  5. 【代码审计】JTBC(CMS)_PHP_v3.0 任意文件上传漏洞分析

      0x00 环境准备 JTBC(CMS)官网:http://www.jtbc.cn 网站源码版本:JTBC_CMS_PHP(3.0) 企业版 程序源码下载:http://download.jtbc. ...

  6. [PyCharm] 设置自动启动时自动打开项目

    设置启动PyCharm时自动打开(或不打开)上次进行的项目: 选择 “Settings - General - Reopen last project on startup”,勾选该选项则启动时自动打 ...

  7. 分析JobInProgress中Map/Reduce任务分配

    1.JobTracker能否决定给当前的TaskTracker节点分配一个Job的具体的哪一个任务? 2.什么是map本地任务? 3.nonRunningMapCache的作用是什么? 4.从Task ...

  8. Repeater的j简单使用

    嘿嘿,今天没有任务,所以突然想起来我之前记得笔 记说要把repeater的使用以及获取值的详细使用总结一下,所以这就闲来无聊总结一下,虽然现在不会使用这些小知识点的,但是我感觉自己的学习还是要 不断地 ...

  9. codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...

  10. 【LeetCode OJ】Merge Two Sorted Lists

    题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...