51nod 字符串连接(贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BBHHTT/article/details/81805750

字符串连接 

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

虽然很简单的一道题,但是没做对。当时想按字典序排序就好了,但是类似b和ba,如果按字典序排是bba,其实应该是bab。那么字符串a,b应该这样比较:a+b<b+a;

#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int cmp(const string &a,const string &b)
{
	return a+b<b+a;
}
int main()
{
	int n;
	string str[105];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		cin>>str[i];
	}
	sort(str,str+n,cmp);
	for(int i=0;i<n;i++)
	{
		cout<<str[i]; 
	}
	printf("\n");
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/BBHHTT/article/details/81805750
今日推荐