Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
来源:互联网 发布:数据库系统是由数据库 编辑:程序博客网 时间:2024/06/07 07:04
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.
Given the list of strings, output the lexicographically smallest concatenation.
Input
The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104).
Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.
Output
Print the only string a — the lexicographically smallest string concatenation.
Examples
input
4abbaabacababcder
output
abacabaabbabcder
input
5xxxxxaxxaaxxaaa
output
xxaaaxxaaxxaxxx
input
3ccbcba
output
cbacbc
思路:一开始的想法是把字符串补齐,然后排个序,但是wa了。看了q神的解法,发现就是在排序的时候用字符串a+b<b+a进行排序,太亮了这方法。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>#define inf 99999999#define pi acos(-1.0)#define maxn 50050#define MOD 1000000007using namespace std;typedef long long ll;typedef long double ldb;struct node{ char s[60];}a[maxn];bool cmp(node a,node b){ char s1[120],s2[120]; char s3[120],s4[120]; strcpy(s1,a.s); strcpy(s2,b.s); strcpy(s3,b.s); strcpy(s4,a.s); strcat(s1,s2); strcat(s3,s4); return strcmp(s1,s3)<0;}int main(){ int n,m,i,j; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++){ scanf("%s",a[i].s); } sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++){ cout<<a[i].s; } cout<<endl; } return 0;}
0 0
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 9 -- C - The Smallest String Concatenation
- Educational Codeforces Round 9 C The Smallest String Concatenation
- Educational Codeforces Round 9 C. The Smallest String Concatenation【思维】
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- Educational Codeforces Round 9-C. The Smallest String Concatenation(string)
- (Educational Codeforces Round 9 )The Smallest String Concatenation(排序)
- string类 sort Educational Codeforces Round 9 C. The Smallest String Concatenation
- Educational Codeforces Round 9 C - The Smallest String Concatenation ,学习到string
- codeforces 632C C The Smallest String Concatenation(排序)
- codeforces 632C The Smallest String Concatenation【巧妙的排序】
- CodeForces 632C The Smallest String Concatenation 排序
- codeforces 632C The Smallest String Concatenation 排序
- The Smallest String Concatenation CodeForces 632C 字符串
- codeforces--623C The Smallest String Concatenation
- Codeforces 632C - The Smallest String Concatenation
- CodeForces-632C-The Smallest String Concatenation
- [object] 与 [object object] 区别!
- SQVI和SAP查询QUERY的区别和使用注意事项
- 提升用户体验之A/B测试(5)——linux安装python(pip)插件
- 《fstab文件字段详解-原理精讲-实战反刍知识回顾》
- 新建并配置Maven工程
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
- 源码编译安装tengine
- Android自定义View(七)--很low的bannerView
- android如何完全root
- POJ-1509 Glass Beads (字符串最小表示法&后缀自动机)
- VS2010环境下C#如何建立与SQLServer的连接并获取数据
- 华为oj iNOC产品部--完全数计算
- 解决VC6.0出现“ Microsoft(R)Developer Studio已停止工作”的问题
- 18. 4Sum