C. The Smallest String Concatenation
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You're given a list of n strings a1, a2, ..., an.
You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.

Given the list of strings, output the lexicographically smallest concatenation.

Input

The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104).

Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50)
consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.

Output

Print the only string a — the lexicographically smallest string concatenation.

Examples
input
4
abba
abacaba
bcd
er
output
abacabaabbabcder
input
5
x
xx
xxa
xxaa
xxaaa
output
xxaaaxxaaxxaxxx
input
3
c
cb
cba
output
cbacbc

题目意思不难懂,但对于我这种大一菜鸟..........任意连接在一起要求字典序最小,数据50000,一般肯定会超时,但在比赛的时候不知道怎么比较字典序,望而生畏,看题解,我去,快要崩溃了,整个人都不好了...

在这里,有个小技巧,以前确实不知道,今天问学长才知道在C++中,用string定义的字符串直接相加”+“就是连接在一起;

下面来看简短代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=50000+10;
string s[N];
int cmp(string a,string b)
{
return a+b<b+a;//c++中string定义的字符串直接相加表示连接;用char定义还需要用strcat()连接;
}
int main()
{
int n,i;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>s[i];
sort(s,s+n,cmp);//万万没想到这样就将字典序最小的连接方式找出来了;
for(i=0;i<n;i++)
cout<<s[i];
cout<<endl;
}
return 0;
}

最新文章

  1. mac svn命令使用
  2. C语言 &#183; 送分啦
  3. 分享20款移动开发中很有用的 jQuery 插件
  4. 基础知识复习(二)——stdafx.h 头文件及x&amp;(x-1)运算
  5. [知乎] 刚开始学习 iOS 开发有什么书推荐呢?
  6. 优化Webstorm
  7. bat面试总结
  8. php定时删除文件夹下文件(清理缓存文件)
  9. 我的MYSQL学习心得 mysql日志
  10. Java实现随意切换VPN改变上网地区
  11. css让文字在一行内显示
  12. vim编辑器介绍及其常用命令
  13. 华为4K机顶盒EC6108V9U从原联通更换为电信的IPTV账号成功经验
  14. Android -- 带你从源码角度领悟Dagger2入门到放弃(一)
  15. Maven项目读取resources下文件的路径问题(getClassLoader的作用)
  16. CefSharp访问需要认证网页或接口(在Request的Headers中添加认证Token)
  17. C指针 的一些练习
  18. c#中在函数后紧跟=&gt;,几个意思,差点懵逼到没有朋友!
  19. linux安装unzip及使用
  20. 循环while 和 continue

热门文章

  1. solr查询优化【转】filtercache
  2. DataTable数据导入DataBase
  3. jQuery选择器之表单元素选择器
  4. 关于Android发送短信获取送达报告的问题
  5. 洛谷 P2515 [HAOI2010]软件安装
  6. Python——集合与字典练习
  7. docker环境安装
  8. magic_quotes_runtime 和 magic_quotes_sybase 的作用
  9. Mathematics-基础:斐波那契数列
  10. 错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java.net.BindException: Address already in use: JVM_Bind