题目描述:
2204 字符串连接
输入n个字符串s[i],你要把他们按某个顺序连接起来,使得字典序最小。
(1 <= n <= 100)
(每个字符串长度 <= 100)
(字符串只包含小写字母)
收起
输入
第一行一个整数n。 接下来每行一个字符串s[i]。输出
一行一个字符串表示把输入的n个字符串按某个顺序连接之后的结果输入样例
6 it looks like an easy problem 4 ba b bc bb输出样例
aneasyitlikelooksproblem babbbbc
思路分析:
bool cmp(const string &a, string &b) {
return a + b < b + a;
}
代码实现:
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e5+100;
string str[N];
bool cmp(string aa,string bb){
return aa+bb<bb+aa;
}
int main(){
int n;
while(cin>>n){
for(int i=1;i<=n;i++)cin>> str[i];
sort(str+1,str+1+n,cmp);
for(int i=1;i<=n;i++)cout<<str[i];
cout<<endl;
}
return 0;
}
THE END;