pat 甲级 1038. Recover the Smallest Number (30)
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 (<=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 思路:可以先考虑其中任意两个字符串s1,s2,若s1+s2<s2+s1,那么s1必须要排在s2的左边才能使得最终结果尽可能的小,所以存在s1+s2>s2+s1的情况就对调s1,s2的位置,最终一定存在稳定状态,使得整个数字最小。这个模拟过程有点像冒泡排序
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 10000+5
typedef long long ll;
string s[N_MAX];
int n;
string process(string s) {
int i = ;
while (i < s.size() && s[i] == '')i++;
string ss = s.substr(i, s.size() - i);
if (!ss.size())ss = "";
return ss;
}
bool cmp(const string &s1,const string &s2) {
return s1+s2 < s2+s1;
}
int main() {
scanf("%d",&n);
for (int i = ; i < n; i++)cin >> s[i];
sort(s, s + n, cmp);
string S="";
for (int i = ; i < n;i++) {
S+=s[i];
}
S = process(S);
cout << S<<endl;
return ;
}
pat 甲级 1038. Recover the Smallest Number (30)的更多相关文章
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 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. ...
- 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
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
随机推荐
- Solr7部署报错:java.lang.NoSuchMethodError: javax.servlet.ServletInputStream.isFinished()Z
错误信息: Servlet.service() for servlet [default] in context with path [/solr] threw exception [Filter e ...
- scrapy 圣墟
# -*- coding: utf-8 -*- import scrapy from sx.items import SxItem class SkSpider(scrapy.Spider): nam ...
- linux中搭建公网ftp服务器
Linux搭建ftp服务器汇总整理 一.检查linux中是否已经安装vsftpd服务端软件 rpm -qa |grep vsftpd 二.卸载linux中的vsftpd服务端软件 rpm -e vsf ...
- linux通配符知识
注意:linux通配符和三剑客(grep,awk,sed)正则表达式是不一样的,因此,代表的意义也是有较大区别的. 通配符一般用户命令行bash环境,而linux正则表达式用于grep,sed,awk ...
- 20.VUE学习之-变异方法push的留言版实例讲解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 单片机入门学习笔记6:新唐单片机N76E003
学习新唐单片机是从2018年3月开始的,之前一点也不懂这一块单片机,之后脉络变的越来越清晰. 由于N76E003档次太低,新塘科技官方的管脚配置,芯片选型……都没有这一块芯片,资料唯独只有:芯片的数据 ...
- Nearest Common Ancestors POJ - 1330 (LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34657 Accept ...
- n个人排队都不站在原来的位置
一.题目描述 有n个人首先站成一排,请问,当n个人第二次再重新排列,每个人都不在原来的位置上,问有多少种站法.例如,原来有3个人,ABC,那么第二次每个人都不在原来的位置上有2种站法,BCA和CAB, ...
- (ADO.NET)关于C#中“配置”sqlite问题
配置打引号,只是因为觉得只是一些小问题,在此记录一下,第一次遇到还真有点手足无措,昨天到今天~终于可以开始放肆的写sqlite了. 好,第一个问题是引用已下载的system.data.sqlite.d ...
- linux centos6 系统优化脚本-经典
转载一篇Ricky的系统优化脚本,这个脚本只能针对centos6x 其他还没有测试,但centos7肯定不行的 #!/bin/bash # ID 201510192126 # Author Ricky ...