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. winform的datagridview控件滚动更新数据

    范例源码下载地址:http://files.cnblogs.com/files/luoxiaozhao/PrintDemo.rar

  2. 第四周——重新clone项目后maven问题

    重新clone项目后,一直报错,"类重复..." clean后install也无效果. 原因是idea在重启项目时会更改maven为默认的idea自带的maven配置,要重新设置

  3. Java中关于注释、标识符、变量、常量、数据类型、类型转换、转移字符以及数值型的表现形式的详解

    Java文件的注意事项 在同一个Java文件中,可以定义多个类,但是被public修饰的类只能够有一个,并且此类名要与文件名一致. 在同一个类中,可以定义多个方法,但是名字叫做main的方法只能有一个 ...

  4. Newtonsoft.Json高级篇:TypeNameHandling设置

    原文:Newtonsoft.Json高级篇:TypeNameHandling设置 此示例使用TypeNameHandling 设置在序列化JSON和读取类型信息时包含类型信息,以便在反序列化JSON时 ...

  5. python基础语法(变量与数据类型)

    python基础语法(变量与数据类型) 一.python变量 python中的变量不需要声明.每个变量在使用钱都需要赋值,变量赋值以后,该变量才会被创建 在python中,变量就是变量,它没有类型,我 ...

  6. wpf Rectangle

    <Rectagle Width="100" Height="100" Stroke="Black" Fill="Blue&q ...

  7. 模拟实现call、apply

    1. 知识点补充: 首先在模拟实现前,先Mark一些我之前不知道的知识: a. eval(string)函数:可计算某个字符串,并执行其中的JavaScript代码 其中,string是必需传入的待计 ...

  8. 【默默努力】PixelFire

    先放下我玩游戏的效果图: 关于游戏最后的结束部分其实我还没有截图,看着挺好看的,后面的效果 再放作者大大的项目地址:https://github.com/panruiplay/PixelFire 接下 ...

  9. [JZOJ5232] 【NOIP2017模拟A组模拟8.5】带权排序

    题目 题目大意 有一个数列AAA,数列上的每个数都是在[li,ri][l_i,r_i][li​,ri​]范围内随机的数. 将这个数列进行稳定排序,得到每个位置在排序后的排名pip_ipi​. f(A) ...

  10. 【JZOJ1259】牛棚安排

    description Farmer John的N(1<=N<=1000)头奶牛分别居住在农场所拥有的B(1<=B<=20)个牛棚的某一个里.有些奶牛很喜欢她们当前住的牛棚,而 ...