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为两数字的长度之和),但是写法很简单。

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; bool Cmp(const string &s, const string &t)
{
string st = s + t;
string ts = t + s;
return st > ts;
} int main(void)
{
int n;
while (cin >> n)
{
if (n == 0) break; vector<string> integers;
int i;
for (i = 0; i < n; ++i)
{
string str;
cin >> str;
integers.push_back(str);
} sort(integers.begin(), integers.end(), Cmp); for (i = 0; i < n; ++i)
{
cout << integers[i];
}
cout << endl;
}
return 0;
}

思路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为两数字串的长度之和)。

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; bool Cmp(const string &s, const string &t)
{
int i;
for (i = 0; i < s.length() && i < t.length(); ++i)
{
if (s[i] != t[i]) return s[i] > t[i];
} if (s.length() == t.length())
{
return true;
}
else if (i == s.length())
{
return Cmp(s, t.substr(i));
}
else
{
return Cmp(s.substr(i), t);
}
} int main(void)
{
int n;
while (cin >> n)
{
if (n == 0) break; vector<string> integers;
int i;
for (i = 0; i < n; ++i)
{
string str;
cin >> str;
integers.push_back(str);
} sort(integers.begin(), integers.end(), Cmp); for (i = 0; i < n; ++i)
{
cout << integers[i];
}
cout << endl;
}
return 0;
}

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

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. C语言 · 陶陶摘苹果

    算法提高 陶陶摘苹果   时间限制:1.0s   内存限制:256.0MB      问题描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出n个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个3 ...

  2. sql server 查找主键、外键、索引、约束

    主键约束 SELECT   tab.name AS [表名],   idx.name AS [主键名称],   col.name AS [主键列名] FROM   sys.indexes idx    ...

  3. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  4. Android APP 分享图片文字到微信刚開始正常,后面就不弹出分享框了

    依照官方的流程走了一遍.一切顺利,分享成功了.本来以为能够大功告成了,结果睡了一觉,第二天要给客户演示了.才发现TMD坑爹了,不能分享了,第三方的分享栏弹不出来了.我一阵惊慌,还好非常快找到了解决的方 ...

  5. scp基本使用方法

    scp基本使用方法: scp用于在两台电脑之间进行数据的copy,形式如下:  第一种, scp [-r] 文件/文件夹  user@host:dir ,需要输入密码.  第二种, scp [-r] ...

  6. Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”

    Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization” 虚拟机Vmware上克隆了一个Red ...

  7. Maqetta源码运行

    一.Maqetta简介 Maqetta是IBM开发的一个创建桌面和移动用户界面的HTML5设计编辑工具.该项目已经捐助给开源机构Dojo基金会.Maqetta提供WYSIWYG可视化HTML5用户界面 ...

  8. CSS圆角框,圆角提示框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Eclipse使用技巧收集

    因为学习Java相关,对Eclipse这个开发工具用的不习惯,许多操作以及快捷键在百度得到解决后下次又忘记了.有时使用VS又混淆了它的快捷键操作.所以写个备忘录持续更新收集平时遇到的操作. 导入项目, ...

  10. (转)x264重要结构体详细说明(1): x264_param_t

    结构体x264_param_t是x264中最重要的结构体之一,主要用于初始化编码器.以下给出了几乎每一个参数的含义,对这些参数的注释有的是参考了网上的资料,有的是自己的理解,还有的是对源代码的翻译,由 ...