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(常用控件:列表视图控件List Control 下)

    转自:http://www.jizhuomi.com/software/197.html 上一节是关于列表视图控件List Control的上半部分,简单介绍了列表视图控件,其通知消息的处理和有关结构 ...

  2. laravel装饰者模式例子

    interface Decorator{ public function display(); } class XiaoFang implements Decorator { private $nam ...

  3. DXP 笔记

    1. 从原理图上添加 net class,快捷键 : P -> V -> C

  4. Python 实现0-1背包

    代码: import numpy as np c=10 #背包容量 w=[2,2,6,5,4] #物品重量 v=[5,3,5,4,6] #物品价值 flag =[0,0,0,0,0] m=np.zer ...

  5. 7_3.springboot2.x启动配置原理_3.事件监听机制

    事件监听机制配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListenerioc容器中的 ...

  6. Hamilton回路 旅行商TSP问题 /// dp oj1964

    题目大意: 给出一个n个顶点的无向图,请寻找一条从顶点0出发,遍历其余顶点一次且仅一次.最后回到顶点0的回路——即Hamilton回路. Input 多测试用例.每个测试用例: 第一行,两个正整数 n ...

  7. 判断语句(if...else)if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码

    判断语句(if...else) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时 ...

  8. 并发和多线程(四)--wait、notify、notifyAll、sleep、join、yield使用详解

    wait.notify.notifyAll 这三个方法都是属于Object的,Java中的类默认继承Object,所以在任何方法中都可以直接调用wait(),notifyAll(),notify(), ...

  9. windows环境下,svn未备份情况下重新恢复

    公司有个同事在未打招呼的情况下把公司服务器进行重新装系统,崩溃啊.SVN之前未备份,还好SVN的库(Repositories)还在,如下图: 恢复办法如下: 由于之前安装的就是VisualSVN-Se ...

  10. MyEclipse使用总结——Maven项目如何启动运行发布到tomcat中[转]

    前面两篇文章: 新建maven框架的web项目 以及 将原有项目改成maven框架 之后,我们已经有了maven的项目 那么 maven项目到底怎么启动呢 如果我们直接在myeclipse中按以前的启 ...