1038 Recover the Smallest Number(贪心,排序)

柔情只为你懂 2024-04-08 12:57 156阅读 0赞

1038 Recover the Smallest Number(贪心)

0、题目

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

  1. 5 32 321 3214 0229 87

Sample Output:

  1. 22932132143287

1、大致题意

给一些字符串,求它们拼接起来构成最小数字的方式

2、基本思路

让我们一起来见证 cmp 函数的强大之处!!~不是按照字典序排列就可以的,必须保证两个字符串构成的数字是最小的才行,所以 cmp函数写成 return a + b < b + a; 的形式,保证它排列按照能够组成的最小数字的形式排列。

因为字符串可能前面有0,这些要移除掉(用s.erase(s.begin())就可以了嗯 string如此神奇)。输出拼接后的字符串即可。

注意:如果移出了0之后发现 s.length() == 0了,说明这个数是0,那么要特别地输出这个0,否则会什么都不输出

3、解题思路

3.1 string 知识点

我在解题前,复习了 string 库的相关知识点,此题主要是 stringerase() 函数,这个函数有2种用法:

  • 删除单个元素
  • 删除一个区间内的所有元素
  • 时间复杂度均为O(N)

    include

    include

    using namespace std;
    int main()
    {

    1. // 删除单个元素
    2. string ss = "12345";
    3. cout << "ss: " << ss << " " << endl;
    4. ss.erase(ss.begin()+4);
    5. cout << "ss.erase(ss.begin()+24: " << ss << endl;
    6. // 删除1个区间内所有元素
    7. cout << endl;
    8. ss = "678910";
    9. cout << "ss: " << ss << " " << endl;
    10. ss.erase(ss.begin()+2, ss.end()-1);
    11. cout << "ss.erase(ss.begin()+2, ss.end()-1): " << ss << endl;
    12. // 从起始位置,删除x个字符
    13. cout << endl;
    14. ss = "678910";
    15. cout << "ss: " << ss << " " << endl;
    16. ss.erase(3,3);
    17. cout << "ss.erase(3,3): " << ss;
    18. return 0;

    }

输出:

  1. ss: 12345
  2. ss.erase(ss.begin()+24: 1234
  3. ss: 678910
  4. ss.erase(ss.begin()+2, ss.end()-1): 670
  5. ss: 678910
  6. ss.erase(3,3): 678
  7. Process returned 0 (0x0) execution time : 0.018 s
  8. Press any key to continue.

3.2 AC代码

  1. #include<vector>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. int n;
  6. vector<string> str;
  7. bool cmp(string a, string b) {
  8. return a+b<b+a;
  9. }
  10. int main() {
  11. cin>>n;
  12. string s;
  13. for(int i=0; i<n; i++){
  14. cin>>s;
  15. str.push_back(s);
  16. }
  17. sort(str.begin(), str.end(), cmp);
  18. string ans;
  19. for(int i=0; i<n; i++)
  20. ans+=str[i];
  21. while(ans.size() != 0 && ans[0] == '0')
  22. ans.erase(ans.begin());
  23. if(ans.size() == 0) cout << 0;
  24. else cout << ans;
  25. return 0;
  26. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,156人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Find the Missing Number

    方法一: 数学方法,先找到最大的值,需要比较最大的值和array size, 要是比array size小, 说明最大值missing。 然后用等差数列公式求得如果不缺失值的和