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 (<=10000) 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. Do not output leading zeros.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287
通过样例可以看到 321 3214 32的顺序 321是 3214子串,很明显应该321在前,大小比较上也是 321 < 3214,而32却在321和3214后面,因为32是3214子串,14明显比32小,323214 比321432大,
所以根据这个去排序,然后凑成一个串去掉前导0.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
string s[];
int n;
bool cmp(string a,string b){
if(a.size() < b.size() && a == b.substr(,a.size()))
{
return a < b.substr(a.size(),b.size());
}
else if(a.size() > b.size() && b == a.substr(,b.size()))
{
return a.substr(b.size(),a.size()) < b;
}
else return a < b;
}
int main() {
string str;
scanf("%d",&n);
for(int i = ;i < n;i ++) {
cin>>s[i];
}
sort(s,s + n,cmp);
for(int i = ;i < n;i ++)
str += s[i];
int i = ;
while(i < str.size() && str[i ++] == '');
i --;
cout<<str.substr(i,str.size());
}

1038 Recover the Smallest Number (30)(30 分)的更多相关文章

  1. 1038 Recover the Smallest Number (30 分)

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

  2. PAT甲1038 Recover the smallest number

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

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

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

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

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

  5. 1038. Recover the Smallest Number (30)

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

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

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

  7. 1038 Recover the Smallest Number (30分)(贪心)

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

  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 (30) - 字符串排序

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

随机推荐

  1. jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色

    jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...

  2. 关于function的一种常用用法

    关于function的一种常用用法 void Share::InitAcrossManager() { GsMgrEvent gsMgrEvents;//保存function的结构体 gsMgrEve ...

  3. Hive高级

    HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...

  4. php自定义函数: 加密下载地址

    function getdownurl($downurl, $extime = "3600", $serverid = 1) { if (empty($downurl)) { re ...

  5. 为什么Git 比 SVN 好

    原文链接:http://www.worldhello.net/2012/04/12/why-git-is-better-than-svn.html Why Git is better than SVN ...

  6. goland中引用包

    首先在工程目录下新建三个目录:bin, pkg,src Bin文件夹是放置编译后的exe文件 Pkg文件夹是放置包生成后的.a文件 Src文件夹是放置包文件的地方 工程组织目录如下,在src中有tes ...

  7. 使用parted 对大容量盘进行分区

    MBR分区表:(MBR含义:主引导记录) 所支持的最大卷:2T (T; terabytes,1TB=1024GB) 对分区的设限:最多4个主分区或3个主分区加一个扩展分区. GPT分区表:(GPT含义 ...

  8. shell按行合并文件

    file1abc file2123 如何合并两个文件,得到a 1b 2c 3 awk方式 awk 'NR==FNR{a[NR]=$0}NR>FNR{print a[FNR],$0}' file1 ...

  9. python基础5 ---python文件处理

    python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...

  10. shell中替换json中指定的值

    在linux中部署软件的时候,有时会遇到用shell动态改动json格式的配置文件,比如一下rabbitmq.json文件: { "rabbitmq": { "ssl&q ...