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
题意:
给出你n个字符串,要你输出n个连在一起的字符串是字典序里最小的。
思路:
这题没有任何思路,看了看大牛的思路,原来只要用到
bool comp(string& a,string& b){ return a+b<b+a;}
这个就可以了。
AC代码:
#include<iostream>
#include<functional>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
#define QWQ ios::sync_with_stdio(0)
typedef unsigned __int64 LL;
typedef __int64 ll;
#define CMP bool cmp(const node& a,const node& b){ return a.R<b.R||(a.R==b.R&&a.L<b.L); }
const int T = 50300;
const int mod = 1000000007;
string s[T];
bool cmp(string& a,string& b){ return a+b<b+a;}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int i,j,k,n,m;
QWQ;
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 << "\n";
}
return 0;
}