C. The Smallest String Concatenation-C++sort排序~~
3 seconds
256 megabytes
standard input
standard output
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
题目意思不难懂,但对于我这种大一菜鸟..........任意连接在一起要求字典序最小,数据50000,一般肯定会超时,但在比赛的时候不知道怎么比较字典序,望而生畏,看题解,我去,快要崩溃了,整个人都不好了...
在这里,有个小技巧,以前确实不知道,今天问学长才知道在C++中,用string定义的字符串直接相加”+“就是连接在一起;
下面来看简短代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=50000+10;
string s[N];
int cmp(string a,string b)
{
return a+b<b+a;//c++中string定义的字符串直接相加表示连接;用char定义还需要用strcat()连接;
}
int main()
{
int n,i;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>s[i];
sort(s,s+n,cmp);//万万没想到这样就将字典序最小的连接方式找出来了;
for(i=0;i<n;i++)
cout<<s[i];
cout<<endl;
}
return 0;
}
C. The Smallest String Concatenation-C++sort排序~~的更多相关文章
- 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 or ...
- CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法
Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them togethe ...
- codeforces 632C C. The Smallest String Concatenation(sort)
C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- C. The Smallest String Concatenation
C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- codeforces 632C. The Smallest String Concatenation 排序
题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vec ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
随机推荐
- Closures闭包
//: Playground - noun: a place where people can play import UIKit /* 闭包 Swift ====> Closures Ruby ...
- jmeter(二十)JMeter中返回Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- C#之九大视图
本节向大家介绍一下有关UML视图方面的内容,UML视图共有9种,它们之间有什么区别和联系呢,下面就让我们一起来学习吧,相信通过本节的介绍你一定会有不少收获. UML视图 UML总共提供了9种视图,这些 ...
- CF782A Andryusha and Socks
题意: Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to ...
- ios MD5大小写加密
#import "NSString+change.h" #import <CommonCrypto/CommonDigest.h> @implementation NS ...
- Android学习笔记(十八) Socket
Socket在计算机网络中的概念是指套接字,包含IP地址和端口号.在Java编程中Socket是一个类,用来实现数据传输(同样可以理解为“套接字”).传输的协议包括UDP和TCP,前者为不可靠的无连接 ...
- mysql出错排查
1,例如:Can't connect to local MySQL server through socket '/tmp/mysql-5.5.37.sock' (2) Mysql链接出错,请配置/A ...
- TensorFlow 安装 Win10 Python+GPU
前叙:有灵魂的程序都是每一个程序员的最终目标.TensorFlow了解下? 打算花几个月学机器学习,TensorFlow是很好的选择,折腾了会环境,略有心得分享下. 环境:win10 Python:3 ...
- 手写MVVM框架 之vue双向数据绑定原理剖析
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Oracle错误 1053: 该服务没有响应启动或控制请求
在服务中,启动oracleDBConsolenewdb服务时,出现了 错误 1053: 该服务没有响应启动或控制请求 在网上查了以后你会发现这是一个非常宽泛的错误,然而我们的建议是去看传说中的orac ...