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. php编译参数选项 具体参数含义可以用./configure --help来查看

    php编译参数选项  PHP_INSTALL_PATH=/data/web/php MYSQL_INSTALL_PATH=/data/web/mysql ./configure --prefix=${ ...

  2. 安装Hadoop 1.1.2 (一 安装JDK)

    1 下载jdk1.7 xxx .rpm 2 以Root权限登陆 3 修改文件权限  chmod +x jdk-7u25-linux-x64.rpm 4 安装 JDK  rpm -ivh jdk-7u2 ...

  3. Spring Ioc (this is my first example)

    一.首先看下源码结构 二.HelloWord 类 package com.northeasttycoon.bean; /** * 打印出 helloword 参数值 * * @author tycoo ...

  4. thinkphp自动验证无效的问题

    新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...

  5. sql server 字符串函数大全

    平常会用到一些函数处理字符串,用的不算频繁,所以每次用到的时候就忘记了,这次在网上找了一篇文档,担心突然某一天这篇文章找不到了,然后就把文章的内容复制了一份: /* 1,ASCII返回字符表达式中最左 ...

  6. task15-18

    [说明]貌似maven在真实的项目实战中挺重要的,可以省去大量的工作,有必要单独学习一下 15.创建一个新的maven项目 16.在src/main/java下随便创建一个java文件,clean,i ...

  7. 【BZOJ3302】[Shoi2005]树的双中心 DFS

    [BZOJ3302][Shoi2005]树的双中心 Description Input 第一行为N,1<N<=50000,表示树的节点数目,树的节点从1到N编号.接下来N-1行,每行两个整 ...

  8. spring boot数据库操作汇总

    1 关于orm orm即object relational mapping,对象关系映射,即将数据库中的表映射成对象. 常用的orm有以下: mybatis spring jdbc template ...

  9. mybatis相关

    1 namespace dao中使用namespace+id一起来完成对mapper中sql statement的调用. 2 关于resultMap和parameterType parameterTy ...

  10. Feign-独立使用-实战

    目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...