时间:2016-03-26 10:32:13 星期六
题目编号:[2016-03-26][codeforces][632][C][The Smallest String Concatenation]
题目大意:给定n个字符串,求把n个字符串拼接起来,使得字符串的字典序最小,输出最后答案
分析:直接排序
排序cmp方法:直接比较 a+b 和 b+a的字典序,小的一定在前面
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn = 5 * 1E4 +10;
char str[maxn][55];
char* pstr[maxn];
bool cmp(string str1,string str2){
string tmp = str1;
str1 += str2;
str2 += tmp;
return str1 < str2;
}
int main(){
int n;
scanf("%d",&n);
getchar();
for(int i = 0;i < n ; ++i){
gets(str[i]);
pstr[i] = str[i];
}
sort(pstr,pstr + n,cmp);
for(int i = 0;i < n ; ++i){
cout<<pstr[i];
}
cout<<'\n';
return 0;
}