【CF# 632C】The Smallest String Concatenation (对string巧妙排序)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/81878000

题干:

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

解题报告:

类似于nyoj 1233这道题(这题需要处理大数)。本题直接排序就好了。这题如果不用string的话,需要用Node结构体存char [] ,然后对Node排序(用strcmp函数),不知道时间上会不会快一点?反正这个题用string类是300+ms左右。

AC代码:

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
string s[50000 + 5];
int dp[1005][1005];
int len1,len2;
bool cmp(string a,string b) {
	return a+b < b+a;
}
int main()
{
	int n;
	cin>>n;
	for(int i = 1; i<=n; i++) {
		cin>>s[i];
	}
	sort(s+1,s+n+1,cmp);
//	for(int i = 1; i<=n; i++) {
//		cout << s[i]<<endl;
//	}
	for(int i = 1; i<=n; i++) {
		cout << s[i];
	}
	
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/81878000