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. nodejs之连接mysql数据库

    一:demo var mysql = require('mysql'); var connection = mysql.createConnection({     host    : '192.16 ...

  2. Activiti流程图查看

    1.测试用例查看图片 public void viewImage() throws Exception { // 创建仓库服务对对象 RepositoryService repositoryServi ...

  3. 关于JAVA项目中CLASSPATH路径详解

    写的不错:http://blog.csdn.net/cheney521/article/details/8672066 以下内容源于复制,把自己觉得不错的东西收集起来: 在dos下编译java程序,就 ...

  4. 关于将ECharts引入到项目中的几种方式

    方式一.在webpack中使用ECharts 1.npm安装ECharts npm install echarts --save 2.引入ECharts 通过 npm 上安装的 ECharts 和 z ...

  5. css---switch开关

    html: <label><input class="mui-switch" type="checkbox"> 默认未选中</la ...

  6. RasieException

    RasieException是SEH API,SEH != 进内核,RasieException并不必然导致用户态内核态切换.事实上这个API被调用以后会首  先尝试在用户态进行处理,如果没有任何处理 ...

  7. Android之FrameLayout帧布局

    1.简介 这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角; 帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看 ...

  8. IDEA中Git的使用(多人合作)

    首先我们要简单知道github跟Git的区别.git是版本控制工具, github是一个面向开源及私有软件项目的托管平台,也是程序员交流的地方. 接下来就开始讲怎么多人一起开发. 首先我们先拥有git ...

  9. codeforces 1186E- Vus the Cossack and a Field

    传送门:QAQQAQ 题意:给一个01矩阵A,他的相反矩阵为B,每一次变换都会将原矩阵面积乘4成为: AB BA 矩阵的左上角固定,变换无限次,现有q个询问,即求一个矩阵内的1的个数. 思路:因为反转 ...

  10. codeforces 1129A2-Toy Train

    传送门:QAQQAQ 题意:有1-n个站点,成环形,有一辆运货车,在这个n个站点之间运输糖果,货车只能按照1->n的方向走,到第n个站的时候,又回到的1,现在告诉你有m个运输任务,从x站点运输一 ...