"蔚来杯"2022牛客暑期多校训练营3 C题

Concatenation

https://ac.nowcoder.com/acm/contest/33188/C

英文题面: NIO was the king of the OIN Kingdom.

He had NN children and wanted to teach them how to count. In the OIN Kingdom, pental is used in counting, so his children can only use 0, 1, 2, 3, and 4 to represent a number.

One day, NIO asked his children to write down their favorite numbers. After that, he came out with a problem: if he concatenated these numbers into a big number, what is the smallest number he could get? However, this problem was too hard for him to solve, so can you help him?

中文题面:

NIO是OIN王国的国王。

他有ññ孩子们,想教他们如何数数。在OIN王国里,pental是用来计数的,所以他的孩子们只能用0、1、2、3、4来代表一个数字。

有一天,蔚来让他的孩子们写下他们最喜欢的数字。之后,他提出了一个问题:如果他将这些数字串联成一个大数,他能得到的最小数字是多少?不过,这个问题对他来说太难解决了,你能帮帮他吗?

题解:

根据题意我们只需要求字典序最小的串即可,即我们排序的时候可以:

bool cmp(string &a, string &b) { return a + b < b + a; }

排序完输出即可,时间复杂度O(nlogn)。

#include<bits/stdc++.h>
using namespace std;
bool cmp(string &a, string &b)
{
    return a + b < b + a;
}
string a[2000100];
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a, a + n, cmp);
    for (int i = 0; i < n; ++i) cout << a[i];
    return 0;
}
全部评论

相关推荐

我:“加班需要有加班工资。”&nbsp;hr:“为什么?”&nbsp;哈哈哈哈哈哈哈离大谱
juntenor:你确实太理想化了,对社会不了解呀。这个和HR没有关系,这是国内特色,不然怎么还会有外包就这种逆天的存在呢。
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务