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. 【很强大的Android图表引擎 - AChartSDK】

    在手机移动App开发中,图表在app中越来越占领举足轻重的地图.而在Android领域.AchartEngine 图表引擎可谓无人不知无人不晓. 可是今天就给各位推荐更为强大的图表引擎. 为什么说更为 ...

  2. python 基础 9.9 查询数据

      #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:21 #@Auther :liuzhenchuan #@File   : ...

  3. MoQ(基于.net3.5,c#3.0的mock框架)简单介绍(转)

    https://www.cnblogs.com/nuaalfm/archive/2009/11/25/1610755.html

  4. The space of such functions is known as a reproducing kernel Hilbert space.

    Reproducing kernel Hilbert space Mapping the points to a higher dimensional feature space http://www ...

  5. 区分Web前端和后端(转载)

    转载自:http://blog.csdn.net/rosetta/article/details/53871766 前言   做C开发将近六年,基本上没有接触过web相关的东西,原来听别人说web相关 ...

  6. Android系统移植与调试之------->如何修改Android设备的桌面背景图片

    1.切换到~/mx0831-0525/device/other/TBDG1073/overlay/frameworks/base/core/res/res目录 2.准备好一张相应尺寸的图片并且命名为d ...

  7. JQuery日记 5.11 Sizzle选择器(五)

    //设置当前document和document相应的变量和方法 setDocument = Sizzle.setDocument = function( node ) { var hasCompare ...

  8. 【oracle案例】ORA-01722

    1.1.   ORA-01722 日期:2014-06-05 14:09 环境:測试环境   [情景描写叙述] 在数据库的升级过程中,运行SQL> @?/rdbms/admin/catupgrd ...

  9. 找出旋转有序数列的中间值python实现

    题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...

  10. 工作中你肯定会有关于 Yii2 的小贴士用法,在下面评论分享出来吧。

    场景: 数据库有user表有个avatar_path字段用来保存用户头像路径 需求: 头像url需要通过域名http://b.com/作为基本url 目标: 提高代码复用 此处http://b.com ...