Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
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.
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.
Print the only string a — the lexicographically smallest string concatenation.
4
abba
abacaba
bcd
er
abacabaabbabcder
5
x
xx
xxa
xxaa
xxaaa
xxaaaxxaaxxaxxx
3
c
cb
cba
cbacbc
题意:给你n个字符串,串的长度可能不同,让你把这些串连起来使得最后的字典序最小。
思路:一开始的想法是把字符串补齐,然后排个序,但是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 1000000007
using 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;
}
Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)的更多相关文章
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I 给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字 ...
- Codeforces Round #445 D. Restoration of string【字符串】
D. Restoration of string time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
随机推荐
- 【剑指Offer】链表的基本操作之创建、插入、删除
// C++ #include<iostream> using namespace std; //链表的定义 struct ListNode { int val; ListNode* ne ...
- 获取json格式的数据变成了undefined!?
今天在做一个简单的登陆功能时,当一切准备就绪,点击登陆时,什么都没发生..然后开始debug,打断点调试,然后发现了这个.向页面传递数据flag是true代表该用户的账号密码验证码等信息正确可以登录, ...
- three.js 之cannon.js物理引擎
今天郭先生说的是一个物理引擎,它十分小巧并且操作简单,没错他就是cannon.js.这些优点都源自于他是基于js编写的,对于js使用者来说cannon.js拥有其他物理引擎没有的纯粹性.从学习成本来看 ...
- 创建一个简单MyBatis程序
文章目录 MyBatis基础 MyBatis 简介 创建一个MyBatis程序 1. 创建Java项目 2. 加载MyBatis包 3. 编写POJO类和映射文件 4.创建mybatis-config ...
- AmoebaNet:经费在燃烧,谷歌提出基于aging evolution的神经网络搜索 | AAAI 2019
论文提出aging evolution,一个锦标赛选择的变种来优化进化算法,在NASNet搜索空间上,对比强化学习和随机搜索,该算法足够简洁,而且能够更快地搜索到更高质量的模型,论文搜索出的Amoeb ...
- kafka(一)入门
一.消息引擎系统 这类系统引以为豪的消息传递属性,像引擎一样,具备某种能量转换传输的能力 消息引擎系统是一组规范,企业利用这组规范在不同系统之间传递语义准确的消息,实现松耦合的异步式数据传递.通俗地讲 ...
- MySQL数据库基础知识及优化
MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...
- 在Ubuntu18.04下编译出ffmpeg(支持推流H265成rtmp)
Ubuntu18.04下编译libx264.libx265.libfdk_aac和ffmpeg 一.编译x264库 二.编译fdk-aac库 三.编译x265库 四.编译FFmpeg源码 五.设置环境 ...
- typora+PicGo+gitee搭建免费的的床
一.gitee 1.第一步拥有自己的gitee账号 没有的可以自己去注册gitee地址 2.使用自己的gitee账号创建仓库 创建好之后注意 记住.com/以后的地址 此处就为y***L/photo- ...
- std::async的使用总结
C++98标准中并没有线程库的存在,直到C++11中才终于提供了多线程的标准库,提供了管理线程.保护共享数据.线程间同步操作.原子操作等类.多线程库对应的头文件是#include <thread ...