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 (≤) 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

 #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//排序问题
int N;
vector<string>v, temp;
string res = "", str = "";
void permuteDFS(int u,vector<bool>&visit)//使用DFS
{
if (u == v.size())
{
for (auto a : temp)
str += a;
res = res > str ? str : res;
str = "";
return;
}
for (int i = ; i < N; ++i)
{
if (visit[i] == true)continue;
visit[i] = true;
temp.push_back(v[i]);
permuteDFS(i + , visit);
temp.pop_back();
visit[i] = false;
}
} void permutex(int u)//使用递归
{
if (u == v.size())
{
for (auto a : v)
str += a;
res = res > str ? str : res;
str = "";
}
for (int i = u; i < N; ++i)
{
swap(v[i], v[u]);
permutex(i + );
swap(v[i], v[u]);
}
} void Sort()//使用排序法则
{
sort(v.begin(), v.end(), [](string a, string b) {return a + b < b + a; });
res = "";
for (auto a : v)
res += a;
} int main()
{
cin >> N;
v.resize(N);
vector<bool>visit(N, false);
for (int i = ; i < N; ++i)
{
cin >> v[i];
res += v[i];
}
//permutex(0);
//permuteDFS(0, visit);
Sort();
while (!res.empty() && res[] == '')
res.erase(, );
if (res.size() == )cout << ;
cout << res << endl;
return ;
}

PAT甲级——A1038 Recover the Smallest Number的更多相关文章

  1. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

  2. pat 甲级 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  3. PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)

    1038 Recover the Smallest Number (30 分)   Given a collection of number segments, you are supposed to ...

  4. A1038. Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  5. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  6. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  7. A1038 Recover the Smallest Number (30 分)

    一.技术总结 此问题是贪心类问题,给出可能有前导零的数字串,将他们按照某个顺序拼接,使生成的数最小. 解决方案,就是使用cmp函数,因为两两字符串进行拼接,进行排序从小到大. 拼接过后会有0可能出现在 ...

  8. PAT_A1038#Recover the Smallest Number

    Source: PAT A1038 Recover the Smallest Number (30 分) Description: Given a collection of number segme ...

  9. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

随机推荐

  1. (4)mysql约束

    完整性约束 1.设置非空约束 not null 简称 NK约束 创建表时设置非空 CREATE TABLE tb_meng2(id INT NOT NULL,age INT); 2.设置字段默认值 默 ...

  2. js加密数据爬取

    - 中国空气质量在线监测分析平台是一个收录全国各大城市天气数据的网站,包括温度.湿度.PM 2.5.AQI 等数据,链接为:https://www.aqistudy.cn/html/city_deta ...

  3. Quartz 定时任务配置(spring中)

    <!-- Quartz -->    <bean name="task" class="com.geostar.geosmarter.nodemanag ...

  4. iOS开发系列-Block本质篇

    概述 在iOS开发中Block使用比较广泛,对于使用以及一些常规的技术点这里不再赘述,主要利用C++角度分析Block内部数据底层实现,解开开发中为什么这样编写代码解决问题. Block底层结构窥探 ...

  5. Foundation框架系列-NSArray

    NSArray常用API 数组字符串指定字符拼接 // 将数组中的元素以separator拼接返回字符串 比如@[@"a=1", @"b=2"] 以separa ...

  6. 廖雪峰Java12maven基础-1maven入门-3构建流程

    maven是一个Java项目管理和构建工具: 标准化项目结构 标准化构建流程(编译.打包.发布) 依赖管理 Maven的构建流程 clean 删除所有编译生成的文件 compile 编译源码.编译测试 ...

  7. 使用 SourceTree 操作时弹出 password required

    通过 https 的方式克隆仓库的,SourceTree 推送等操作的时候会弹出提示要求输入密码. 在仓库里面设置: 远程仓库-选中仓库-点击编辑-修改 url 路径, 路径格式,以码云为例: htt ...

  8. spark Infinate 的处理

    去掉infinity数据的方法: absperrordf_rdd = absperrordf.rdd.filter(lambda x: (np.isinf(float(x.avgperror)) == ...

  9. 【转】浅析BFC及其作用

    1. 什么是BFC BFC(block formatting context):简单来说,BFC 就是一种属性,这种属性会影响着元素的定位以及与其兄弟元素之间的相互作用. 中文译为块级格式化上下文.是 ...

  10. Spring简洁总结

    Spring简洁总结  要的对象不是自己建的,而是IOC容器(XML文件)给的,我们通过getbean来调用. 依赖注入的话就是对象(bean)的成员的赋值不是我们手动完成,而是容器(XML文件)给我 ...