4thIIUCInter-University Programming Contest, 2005

A

Children’s Game

Input: standard input
Output: standard output

Problemsetter: Md. Kamruzzaman

There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be givenN positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made – 1231245690, 1241235690, 5612312490, 9012312456, 9056124123 etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made.

You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?

Input

Each input starts with a positive integer N (≤ 50). In next lines there areN positive integers. Input is terminated by N = 0, which should not be processed.

Output

For each input set, you have to print the largest possible integer which can be made by appending all theN integers.

Sample Input

Output for Sample Input

4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0

9056124123
99056124123
99999

思路1:

贪心原则,为了让最终得到的数尽可能大,所以要让高位的数值尽可能大,直接按反字典序将数字串排序,并不正确,比如9和90排序后是90在前。

不过两个数字排列方式无非是一前一后,所以可以把两种排列的结果都比较下,这样做的的时间复杂度(只是两个数的比较)为O(3n),同时要借助2n的空间(n为两数字的长度之和),但是写法很简单。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. bool Cmp(const string &s, const string &t)
  9. {
  10. string st = s + t;
  11. string ts = t + s;
  12. return st > ts;
  13. }
  14.  
  15. int main(void)
  16. {
  17. int n;
  18. while (cin >> n)
  19. {
  20. if (n == 0) break;
  21.  
  22. vector<string> integers;
  23. int i;
  24. for (i = 0; i < n; ++i)
  25. {
  26. string str;
  27. cin >> str;
  28. integers.push_back(str);
  29. }
  30.  
  31. sort(integers.begin(), integers.end(), Cmp);
  32.  
  33. for (i = 0; i < n; ++i)
  34. {
  35. cout << integers[i];
  36. }
  37. cout << endl;
  38. }
  39. return 0;
  40. }

思路2:

假设如下数字串:

A: a1a2a3…an

B: b1b2b3…bm

AB: a1a2a3…anb1b2b3…bm

BA: b1b2b3…bma1a2a3…an

判断两数字串谁应该放在前面时(这里把字符串应该放在前面的称为较大数字串),有两种情况

1、  A和B能够直接比较出大小

当比较到ax和bx时(x<=n且x<=m),分出大小。

2、  需要对AB和BA的值进行比较

AB: a1a2a3…anb1b2b3…bm

BA: b1b2b3…bma1a2a3…an

比较到am和bm时(m<n),仍然相等,这时说明

a1a2a3…am= b1b2b3…bm

接下来从AB式中的am+1和BA式中的a1进行比较

AB1:am+1am+2…anb1b2b3…bm

BA1:a1a2a3…an

用①式对BA1进行替换,得到

AB1:am+1am+2…anb1b2b3…bm

BA1:b1b2b3…bm am+1am+2…an

可以发现AB1和BA1与AB和BA的形式完全相同,只是下标的值不同而已,所以这个比较过程是递归的。

因为整个比较过程中,AB和BA中总是有一个下标在向后走,所以时间复杂度为O(n)(n为两数字串的长度之和)。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. bool Cmp(const string &s, const string &t)
  9. {
  10. int i;
  11. for (i = 0; i < s.length() && i < t.length(); ++i)
  12. {
  13. if (s[i] != t[i]) return s[i] > t[i];
  14. }
  15.  
  16. if (s.length() == t.length())
  17. {
  18. return true;
  19. }
  20. else if (i == s.length())
  21. {
  22. return Cmp(s, t.substr(i));
  23. }
  24. else
  25. {
  26. return Cmp(s.substr(i), t);
  27. }
  28. }
  29.  
  30. int main(void)
  31. {
  32. int n;
  33. while (cin >> n)
  34. {
  35. if (n == 0) break;
  36.  
  37. vector<string> integers;
  38. int i;
  39. for (i = 0; i < n; ++i)
  40. {
  41. string str;
  42. cin >> str;
  43. integers.push_back(str);
  44. }
  45.  
  46. sort(integers.begin(), integers.end(), Cmp);
  47.  
  48. for (i = 0; i < n; ++i)
  49. {
  50. cout << integers[i];
  51. }
  52. cout << endl;
  53. }
  54. return 0;
  55. }

欢迎转载,但请尊重作者,标明出处。

UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)的更多相关文章

  1. uva 10905 Children's Game (排序)

    题目连接:uva 10905 Children's Game 题目大意:给出n个数字, 找出一个序列,使得连续的数字组成的数值最大. 解题思路:排序,很容易想到将数值大的放在前面,数值小的放在后面.可 ...

  2. UVA 10905 Children's Game 孩子的游戏 贪心

    题意:给出N个数,要求把它们拼凑起来,让得到的数值是最大的. 只要分别比较两个数放前与放后的值的大小,排序后输出就可以了. 比如123和56,就比较12356和56123的大小就行了. 写一个比较函数 ...

  3. 【字符串排序,技巧!】UVa 10905 - Children’s Game

    There are lots of number games for children. These games are pretty easy to play but not so easy to ...

  4. UVA 10905 Children's Game (贪心)

    Children's Game Problem Description There are lots of number games for children. These games are pre ...

  5. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. UVa 10905 Children's Game

    注意!这不是单纯的字典序排序,比如90.9,应该是990最大 对字符串排序蛋疼了好久,因为别人说string很慢,所以一直没有用过. 看别人用string还是比较方便的,学习一下 对了,这里的cmp函 ...

  7. UVA 10905 Children's Game (贪心)

    贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...

  8. hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  9. C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55

    //C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55 using System; using System.Collections.Generic; using S ...

随机推荐

  1. hive & hive beeline常用参数

    Hive 1参数如下: usage: hive -d,--define <key=value> Variable substitution to apply to Hive command ...

  2. lua中table如何安全移除元素

    在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...

  3. Java 中的异常和处理详解

    Java 中的异常和处理详解 原文出处: 代码钢琴家 简介 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常.异常发生时,是任程序自生自灭,立刻退出终止,还是输出错误 ...

  4. json数据 提示框flash.now[:notice] flash.now[:alert]

    实现json.做出提示框 1.在controller中使用flash.now[:alert] = "str"方法来做print def topodata #@vnic = Vnic ...

  5. EasyUI的treegrid组件动态加载数据问题的解决办法

    http://www.jquerycn.cn/a_3455 —————————————————————————————————————————————————————————————————————— ...

  6. php 进度条

    <?php header( 'Content-type: text/html; charset=utf-8' ); echo 'Begin ...<br />'; for( $i = ...

  7. spring复习 -day1:Spring简介 、 Spring容器 、 Spring IOC

    Spring创建对象方法   创建对象过程: 第一步:添加SpringIOC环境 (1)在WebRoot/WEB-INT/lib文件夹下,引入SpringIOC配置环境的jar包 如图: (2)在sr ...

  8. 【转】WCF入门教程五[WCF的通信模式]

    一.概述 WCF在通信过程中有三种模式:请求与答复.单向.双工通信.以下我们一一介绍. 二.请求与答复模式 描述: 客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务 ...

  9. 【转】MFC 数据绑定 DoDataExchange( )

    void CRegisterDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DAT ...

  10. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...