Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
来源:互联网 发布:mac 图片导入后找不到 编辑:程序博客网 时间:2024/06/14 04:21
题目链接:http://codeforces.com/problemset/problem/632/C
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.
4abbaabacababcder
abacabaabbabcder
5xxxxxaxxaaxxaaa
xxaaaxxaaxxaxxx
3ccbcba
cbacbc
题解:
1.设有两个字符串a和b:求他们最小字典序的组合,那么只有两种情况 : a+b 和 b+a,即取字典序小的那个。
2.字符串个数由两个推广到n个,那么即相当于在原基础上插入新的字符串,使得字典序最小。所以这是一个排序的过程。
代码如下:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 5e4+10;string s[maxn];int n;bool cmp(string a, string b){ return a+b<b+a;}int main(){ cin>>n; for(int i = 1; i<=n; i++) cin>>s[i]; sort(s+1,s+1+n,cmp); for(int i = 1; i<=n; i++) cout<<s[i]; cout<<endl; return 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)
- string类 sort 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(排序)
- The Smallest String Concatenation CodeForces 632C 字符串
- codeforces--623C The Smallest String Concatenation
- Codeforces 632C - 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
- The Smallest String Concatenation CodeForces
- MySQL数据库主从同步存在问题的分析与学习
- 路往何方
- codeforces 270A Fancy Fence
- 设计模式之责任链模式(Chain of Responsibility)
- 利用数组和for循环给一维数组内n个元素大小排序
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- firewalld
- QML中圆形进度仪表盘控件的使用
- TensorFlow风格指南
- Eclipse智能提示及快捷键
- vue.js webpack环境配置介绍
- WEB常用测试用例—文本输入框
- OutOfMemoryError: GC overhead limit exceeded
- Git仓库备份