1038. Recover the Smallest Number (30)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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
 
我做题都是先用python秒一下,要是超时什么的就换C++了,这题我一看,就写了下面这一行代码:
print int(''.join(sorted(raw_input().split()[:], lambda s1, s2: cmp(s1+s2, s2+s1))))

是的只有一行!可惜最后一个test还是超时了。于是用C++再写:

#include <set>
#include <sstream>
#include <string>
#include <iostream>
using namespace std; class mstring : public string {
public:
inline bool operator<(const string &s) const {
string tmpa(*this);
tmpa.append(s);
string tmpb(s);
tmpb.append(*this);
return tmpa.compare(tmpb) < ;
}
}; int main() {
//#pragma warning(disable:4996)
//freopen("..\\advanced-pat-python\\test.txt", "r", stdin);
int N;
cin >> N;
set<mstring> nums;
mstring ms;
while (N--) {
cin >> ms;
nums.insert(ms);
}
mstring num;
set<mstring>::iterator it = nums.begin();
while (it!=nums.end()) {
num += *(it++);
}
while (!num.empty() && *num.begin()=='') {
num.erase(num.begin());
}
if (num.empty()) {
num += '';
}
cout << num << endl;
}

总体来说还是较为简单的,但是没有Python那么爽的一行解决掉了。Python真是神奇的语言啊。

PAT 1038 体验Python之美的更多相关文章

  1. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  2. 感受python之美,python简单易懂的小例子

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 1 简洁之美 通过一行代码,体会Python语言简洁之美 2 Python ...

  3. Python之美--Decorator深入详解

    转自:http://www.cnblogs.com/SeasonLee/archive/2010/04/24/1719444.html 一些往事 在正式进入Decorator话题之前,请允许我讲一个小 ...

  4. UliPad 初体验----python 开发利器

    学习python 有段时间,最近博客更新比较慢了,空闲时间在零零碎碎的学python ,难成文,也就没整理成博客. 学习python 最苦恼的就是没有趁手IDE ,之前学java 时 Eclipse  ...

  5. Python之美[从菜鸟到高手]--深刻理解原类(metaclass)

    本来想自己写这篇文章的,可当我读了这篇文章http://blog.jobbole.com/21351/,我打消了这个念头,因为肯定写的没有人家的好,说的通俗易懂,面面俱到.就厚着面皮修改下格式,测试下 ...

  6. Python之美[从菜鸟到高手]--生成器之全景分析

    yield指令,可以暂停一个函数并返回中间结果.使用该指令的函数将保存执行环境,并且在必要时恢复. 生成器比迭代器更加强大也更加复杂,需要花点功夫好好理解贯通. 看下面一段代码: def gen(): ...

  7. Python之美[从菜鸟到高手]--urlparse源码分析

    urlparse是用来解析url格式的,url格式如下:protocol :// hostname[:port] / path / [;parameters][?query]#fragment,其中; ...

  8. 浙大 pat 1038 题解

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

  9. PAT 1038 统计同成绩学生(20)(代码)

    1038 统计同成绩学生(20)(20 分) 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过10^5^的正整数N,即学生总人数.随后1行给出N名学生 ...

随机推荐

  1. 【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度

    1002: [FJOI2007]轮状病毒 Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同 ...

  2. 2、分布式文件系统---HDFS

    1.HDFS设计前提与目标 (1)硬件错误是常态而不是异常.  错误检测并快速自动恢复是HDFS最核心设计目标 (2)流式数据访问.运行在HDFS上的应用主要是以流式数据读取为主,做批量处理而不是用户 ...

  3. Winodws live writer

    发布一篇试试.

  4. spring IOC源码分析(3)

    1.IOC容器的依赖注入 Spring中,依赖注入是在用户第一次向IOC容器索要Bean时触发的(通过getBean方法). 在BeanFactory中我们看到getBean(String…)函数,它 ...

  5. splay学习笔记

    伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.(来自百科) 伸展树的操作主要是 –rotate(x) 将x旋转到x的父亲的位置 voi ...

  6. YARN加载本地库抛出Unable to load native-hadoop library解决办法

    YARN加载本地库抛出Unable to load native-hadoop library解决办法 用官方的Hadoop 2.1.0-beta安装后,每次hadoop命令进去都会抛出这样一个War ...

  7. 【Linux远程管理】RDP协议远程管理

    RDP(Remote Desk Protocol).远程桌面协议,常用的Windows操作系统的远程桌面管理就就是基于该协议. 而在Linux下,我们也是可以找到开源的rdp server的,这就是x ...

  8. CentOS镜像163更新源

    首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...

  9. LinuxShell_variable+if+while

    [root@ossec-server mybash]# vim ./hello.sh #! /bin/sh # This is a example bash script echo "Hel ...

  10. [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]

    参考: 1. 郭华阳 - 算法合集之<RMQ与LCA问题>. 讲得很清楚! 2. http://www.cnblogs.com/lazycal/archive/2012/08/11/263 ...