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. ...
随机推荐
- TensorFlow基础笔记(4) Tensor Transformation
https://segmentfault.com/a/1190000008793389 抽取 tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连 ...
- MyBatis的优缺点
优点: 1.简单易学 mybatis本身就很小且简单.没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现 ...
- 在JAVA中利用public static final的组合方式对常量进行标识
在JAVA中利用public static final的组合方式对常量进行标识(固定格式). 对于在构造方法中利用final进行赋值的时候,此时在构造之前系统设置的默认值相对于构造方法失效. 常量(这 ...
- 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序(置换群)
http://www.lydsy.com/JudgeOnline/problem.php?id=1697 置换群T_T_T_T_T_T_T 很久以前在黑书和白书都看过,,,但是看不懂... 然后找了本 ...
- MFC常见错误提示:opened in another editor
有时候在使用MFC的过程中常常会遇到这种提示.假设你在看想必你也遇到这种烦恼. 没办法-- 把打开的文件所有关闭.然后你就发现期待的RC文件出现了! .!!!.!!!!!!! ! !.! !! .
- Sublime Text 模版插件: SublimeTmpl
开发者的插件介绍页面:http://www.fantxi.com/blog/archives/sublime-template-engine-sublimetmpl/ 写了个sublime的模版插件, ...
- Linux 文件夹含义(转)
1./bin :获得最小的系统可操作性所需要的命令 2./boot :内核和加载内核所需的文件 3./dev :终端.磁盘.调制解调器等的设备项 4./etc :关键的启动文件和配置文件 5./hom ...
- videoview 播放视频
-videoVIew 继承SurfaceView 使用android的VideoView来播放一个视频,步骤是: 1,在xml中创建一个videoView, 2,在java中导入,然后创建一个Medi ...
- iOS7Status bar适配
一 Status bar重叠问题: - Zherui if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7.0) { sel ...
- [Web] 如何实现Web服务器和应用服务器的负载均衡?
本文对Web服务器和应用服务器的负载均衡进行说明. 在负载均衡的思路下,多台服务器为对称方式,每台服务器都具有同等的地位,可以单独对外提供服务而无须其他服务器的辅助.通过负载分担技术,将外部发送来的请 ...