字符串连接
wwwwodddd (命题人)
基准时间限制:1 秒 空间限制:131072 KB 分值: 5
输入n个字符串s[i],你要把他们按某个顺序连接起来,使得字典序最小。
(1 <= n <= 100)
(每个字符串长度 <= 100)
(字符串只包含小写字母)
Input
第一行一个整数n。 接下来每行一个字符串s[i]。
Output
一行一个字符串表示把输入的n个字符串按某个顺序连接之后的结果
Input示例
6 it looks like an easy problem
Output示例
aneasyitlikelooksproblem
做的时候以为就是每个字符串按照字典序排然后输出就好了,忘了考虑空字符串是大还是小。
举两个例子
排序:如果是用return a<b,大家可以考虑一下 b ba和b bc (无法知道空字符是大还是小)
其实只要判断a+b和b+a就好了
AC代码:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
bool cmp(const string&a,const string&b)
{
return a+b<b+a;
}
int main()
{
string s[101];
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>s[i];
sort(s,s+n,cmp);
for(int i=0;i<n;i++)
cout<<s[i];
cout<<endl;
return 0;
}