PAT 1038 体验Python之美
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之美的更多相关文章
- Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)
我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...
- 感受python之美,python简单易懂的小例子
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 1 简洁之美 通过一行代码,体会Python语言简洁之美 2 Python ...
- Python之美--Decorator深入详解
转自:http://www.cnblogs.com/SeasonLee/archive/2010/04/24/1719444.html 一些往事 在正式进入Decorator话题之前,请允许我讲一个小 ...
- UliPad 初体验----python 开发利器
学习python 有段时间,最近博客更新比较慢了,空闲时间在零零碎碎的学python ,难成文,也就没整理成博客. 学习python 最苦恼的就是没有趁手IDE ,之前学java 时 Eclipse ...
- Python之美[从菜鸟到高手]--深刻理解原类(metaclass)
本来想自己写这篇文章的,可当我读了这篇文章http://blog.jobbole.com/21351/,我打消了这个念头,因为肯定写的没有人家的好,说的通俗易懂,面面俱到.就厚着面皮修改下格式,测试下 ...
- Python之美[从菜鸟到高手]--生成器之全景分析
yield指令,可以暂停一个函数并返回中间结果.使用该指令的函数将保存执行环境,并且在必要时恢复. 生成器比迭代器更加强大也更加复杂,需要花点功夫好好理解贯通. 看下面一段代码: def gen(): ...
- Python之美[从菜鸟到高手]--urlparse源码分析
urlparse是用来解析url格式的,url格式如下:protocol :// hostname[:port] / path / [;parameters][?query]#fragment,其中; ...
- 浙大 pat 1038 题解
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 1038 统计同成绩学生(20)(代码)
1038 统计同成绩学生(20)(20 分) 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过10^5^的正整数N,即学生总人数.随后1行给出N名学生 ...
随机推荐
- Vijos p1165 火烧赤壁 离散化+单调栈
题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...
- vm安装ubuntu桥接模式无法联网
桥接模式,就是和主机不同的ip,其他都是一样的. 编辑网络连接 查看自己机子的ip ipconfig 一般情况下是 ip 192.168.1.XXX 子网掩码 255.255.255.0 网关 192 ...
- Memcached(七)Memcached的并发实例
1. Memcached是什么?Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么?Memcached是基于Key/Value对的HashMap.每一对,都 ...
- Memcached(四)Memcached的CAS协议
1. 什么是CAS协议很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS是 ...
- 转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)
maven打包时始终出现以下提示: 1.-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)List<User> userList= new Array ...
- 查看java的.class文件的方法
在不通过eclipse等IDE安装反编译插件的情况下查看java的.class文件的方法:可以通过下载jd-gui class文件查看工具进行查看.如附件的“jd-gui.exe”程序. 1. 从网上 ...
- plsql exist和in 的区别
<![endif]--> <![endif]--> 发现公司同事很喜欢用exists 和in 做子查询关联,我觉得很有必要研究下 两者的区别,供参考和备忘 /* (这段信息来自 ...
- 存储过程系列之存储过程sql查询存储过程的使用
1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select ob ...
- 【POJ】1692 Crossed Matchings
经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...
- 近期会放出tlplayer for android的更新版本
tlplayer for android的一次重大更新在近期将会放出,自从去年初的时候放出tlplayer android版本后,一直都没有更新tlplayer,而tlplayer for windo ...