2204 字符串连接(经典贪心:构造最小字典序的串,同 1097)

题目描述:

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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值