PAT 1038 Recover the Smallest Number[dp][难]
1038 Recover the Smallest Number (30 分)
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 (≤104) 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. Notice that the first digit must not be zero.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
题目大意:给出几个数,要求求出它们的片段拼接,并且这个数是所有数中最小的。
//一看到就不太明白怎么做,拼接不同总长度也不一定相同,有0开头的,如果放在中间的话就算是中间一位了。没什么思路,考试遇到这个的话会跪。
代码来自:https://www.liuchuo.net/archives/2303
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp0(string a, string b) {//两个相同的数相加,它们的长度肯定是相同的。
return a + b < b + a;
}
string str[];//使用字符串数组!
int main() {
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
cin >> str[i];//以String作为输入。
sort(str, str + n, cmp0);
string s;
for(int i = ; i < n; i++)
s += str[i];//将字符串拼接。
while(s.length() != && s[] == '')
s.erase(s.begin());//将开头的0除去。
if(s.length() == ) cout << ;
cout << s;
return ;
}
//柳神的代码简直叹为观止。厉害了,学习了。
1.对字符串的处理,关键是这个cmp0函数的使用。
PAT 1038 Recover the Smallest Number[dp][难]的更多相关文章
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- PAT 1038. Recover the Smallest Number
#include <iostream> #include <cstdlib> #include <vector> #include <algorithm> ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 1038 Recover the Smallest Number (30 分)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1038. Recover the Smallest Number (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT 甲级 1038 Recover the Smallest Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...
- PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
随机推荐
- mui区域滚动条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- xsocket:空闲超时问题。
XSocket是什么? java的nio的封装. 详情: 1. http://xsocket.sourceforge.net/core/apidocs/2_1/index.html 2. http:/ ...
- poj 1090:The Circumference of the Circle(计算几何,求三角形外心)
The Circumference of the Circle Time Limit: 2 Seconds Memory Limit: 65536 KB To calculate the c ...
- SVN入门 服务器VisualSVN Server和客户端TortoiseSVN安装
Subversion是一个版本控制系统,相对于的RCS.CVS,采用了分支管理系统,它的设计目标就是取代CVS.互联网上免费的版本控制服务多基于Subversion. 一.SVN工作原理 SVN(Su ...
- uva 610(tarjan的应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23727 思路:首先是Tarjan找桥,对于桥,只能是双向边,而对于 ...
- server.xml详解
http://www.cnblogs.com/gugnv/archive/2012/02/01/2334187.html http://blog.csdn.net/weinianjie1/articl ...
- Sublime Text2.0.2注冊码
// Sublime Text 3 License Keys // Sublime Text 2.x -– BEGIN LICENSE -– Andrew Weber Single User Lice ...
- GIS Cesium地图数据配置
1.打开IIS,点击站点 2.跨域配置 配置方式: 在地图数据目录之中放置web.config文件,里面存放 <?xml version="1.0" encoding=&qu ...
- 本地存储数据库indexedDB实现离线预览的功能
今天在学习<高级JS编程>,看到离线存储,cookie和session都十分的熟悉,但是书中还提到了indexedDB和webSQL(已废弃),indexedDB可以像mysql一样建表, ...
- JDBC通用DAO
dbcBaseDao接口,内容如下: package com.sun4j.core.jdbc.dao; import java.io.Serializable; import java.util.Li ...