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. button的相关属性

    设置自定义按钮的文字大小 [submit.titleLabel setFont:[UIFont boldSystemFontOfSize:16]]; 设置按钮选中状态的颜色 [btn setTintC ...

  2. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  3. Python的字符串操作和Unicode

    字符串类型 str:Unicode字符串.采用''或者r''构造的字符串均为str,单引号可以用双引号或者三引号来代替.无论用哪种方式进行制定,在Python内部存储时没有区别. bytes:二进制字 ...

  4. Xplico

    http://zhulinu.blog.51cto.com/539189/850909

  5. UVALive 5903 Piece it together

    一开始用的STL一直超时不能过,后来发现AC的代码基本都用的普通邻接表,然后改了一下13s,T=T,效率太低了.然后把某大神,详情戳链接http://acm.hust.edu.cn/vjudge/pr ...

  6. 手游:cocos2d-x3.0 移植 wp8 开发 各种 “蛋疼”问题的汇总

    蛋疼的问题的起源: wp8 做应用开发,显示显示中文,源码包含中文都是没有一点问题的, 只是cocos2d-x 的编码方式(UTF-8),引起的一系列的问题. 1:不能显示服务器返回的中文 2:c++ ...

  7. [unity菜鸟] 笔记1 —— 函数篇

    SendMessage() 调用其他物体中的指令,先在脚本中编写一个自定义的函数,然后使用SendMessage()命令来调用那个物体上的命令 //①将以下函数附给target对象 void Rena ...

  8. MFC浅析(7) CWnd类虚函数的调用时机、缺省实现

    CWnd类虚函数的调用时机.缺省实现 FMD(http://www.fmdstudio.net) 1. Create 2. PreCreateWindow 3. PreSubclassWindow 4 ...

  9. 《ArcGIS Engine+C#实例开发教程》第五讲 鹰眼的实现

    原文:<ArcGIS Engine+C#实例开发教程>第五讲 鹰眼的实现 摘要:所谓的鹰眼,就是一个缩略地图,上面有一个矩形框,矩形框区域就是当前显示的地图区域,拖动矩形框可以改变当前地图 ...

  10. perl 正则匹配多个

    Vsftp:/root# cat k1.pl my $_='upDaTe'; if( $_ =~ /^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK| ...