zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串

    题目链接:http://codeforces.com/problemset/problem/632/C


    C. The Smallest String Concatenation
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    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
    4
    abba
    abacaba
    bcd
    er
    
    output
    abacabaabbabcder
    
    input
    5
    x
    xx
    xxa
    xxaa
    xxaaa
    
    output
    xxaaaxxaaxxaxxx
    
    input
    3
    c
    cb
    cba
    
    output
    cbacbc


    题解:

    1.设有两个字符串a和b:求他们最小字典序的组合,那么只有两种情况 : a+b 和 b+a,即取字典序小的那个。

    2.字符串个数由两个推广到n个,那么即相当于在原基础上插入新的字符串,使得字典序最小。所以这是一个排序的过程。



    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 5e4+10;
     9 
    10 string s[maxn];
    11 int n;
    12 
    13 bool cmp(string a, string b)
    14 {
    15     return a+b<b+a;
    16 }
    17 
    18 int main()
    19 {
    20     cin>>n;
    21     for(int i = 1; i<=n; i++)
    22         cin>>s[i];
    23 
    24     sort(s+1,s+1+n,cmp);
    25     for(int i = 1; i<=n; i++)
    26         cout<<s[i];
    27     cout<<endl;
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    angular 中同级元素交替样式
    Type反射遍历类的属性
    对路径“xxxxx”的访问被拒绝。
    判断文件路径和文件是否存在
    List集合删除方法
    .NET 树型递归
    AngularJS使用ngMessages进行表单验证
    Windows 端口占用查询
    小程序页面高度控制
    如何理解多个域名解析到同一个服务器空间上?
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538673.html
Copyright © 2011-2022 走看看