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. VS2010-MFC(对话框:向导对话框的创建及显示)

    转自:http://www.jizhuomi.com/software/166.html 上一节讲了属性页对话框和相关的两个类CPropertyPage类和CPropertySheet类,对使用属性页 ...

  2. POJ-2752-Seek the Name-kmp的变形

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...

  3. PAT甲级——A1138 Postorder Traversa【25】

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  4. 第二周课堂笔记1th

    1.    三元运算 + 2.      for循环 for为有限循环,while为无限循环 可迭代对象:是字符串,数字不可以 数字不可以迭代但是可以用range函数 for i in range(1 ...

  5. 使用APOC技术从MYSQL数据库导数据到Neo4j图数据库(JDBC)

                                                     Neo4j 数据导入 一.安装与部署Neo4j 直接在官网下载安装包安装,解压即可. 2.mysql ...

  6. css 图片波浪效果

    参考:https://blog.csdn.net/zhichaosong/article/details/80944924#_99 效果: wave2.png html: <!DOCTYPE h ...

  7. 内核下枚举进程 (二)ZwQuerySystemInformation

    说明: SYSTEM_INFORMATION_CLASS 的5号功能枚举进程信息.其是这个函数对应着ring3下的 NtQuerySystemInformation,但msdn上说win8以后ZwQu ...

  8. python学院体系

  9. LoadRunner参数化详解【转】

    距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...

  10. configparser 配置文件模块

    #_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...