码迷,mamicode.com
首页 > 其他好文 > 详细

hrbust 训练赛 SUG 347

时间:2017-10-15 19:26:29      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:length   tin   nali   follow   find   int   logs   str1   size   

链接:

http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=31748

题目:

Description


His Royal Highness King of Berland Berl XV was a very wise man and had a very accomplished wife, who was aware of the fact, that prominent and outstanding personalities once having written down their names on the pages of glorious History, remain there forever. His Royal Highness King Berl XV experienced an intrinsic, lost nowadays, deep and sincere sense of respect and trust for his beloved spouse. So he decided to acquire a chronicler of his own. Due to the ambiguous nature of misunderstanding and the crying injustice of history to ambiguity, he decided to leave all his royal responsibilities aside and made up his royal mind to find the chronicler, who will make him famous, depicting all his heroic deeds truthfully and gloriously enough.

The King assembled the greatest minds of his kingdom at the Academic Chroniclers Meeting (ACM), as he named it, and decided to test their might. The task was to build the Smallest Lexicographical Concatenation (SLC) out of the given N strings. SLC of N strings s1,..., sN is the lexicographically smallest their concatenation si1 +... + siN, where i1,..., iN is a permutation of integers from 1 through N. It‘s a great privilege to be a chronicler, so don‘t miss your chance and don‘t screw it up! Make the king choose you!

Input
The first line of the input file contains a single integer N (1 ≤ N ≤ 100) indicating the number of strings. The following N lines contain N strings, one string per line. The length of each string is no more than 100 characters. Each string consists only of lowercase Latin letters. There are no any leading or trailing spaces.

Output
Print the SLC of the given N strings to the output file as a single line.

Sample input
6
it
looks
like
an
easy
problem


Sample output


aneasyitlikelooksproblem

解题思路:

题意:(主要是上文的红色字体)有道翻译:
任务是在给定的N字符串中构建最小的字典拼接(SLC)。

N个字符串的SLC,…,sN 是字典上最小的连接 Si1 +…Sin,  i1,……in是一个从1到n的整数排列,

这是一个很荣幸的编年史作家,所以不要错过你的机会,不要搞砸了!让国王选择你!

解法:  区别 “按字典序排序”  和 “字典序上最小的连接”

        按字典序即顺序 a-z (A-Z)输出

        字典序上最小的连接:比如串 str1 = ‘ab‘ ,str2 = ‘ba‘,则 str1 + str2 = ‘abba’ ,

        而str2 + str1 = ‘baab’,所以第二种要比第一种小

代码:

#include<bits/stdc++.h>
using namespace std;

bool cmp(string str1,string str2)
{
    return str1 + str2 < str2 + str1;
}

int main()
{
    int n;
    scanf("%d",&n);
    string s[105];
    for(int i = 0; i < n; i++)
    {
        cin>>s[i];
    }
    sort(s,s+n,cmp);
    for(int i = 0; i < n; i++)
    {
        cout<<s[i];
    }
    return 0;
}

 

hrbust 训练赛 SUG 347

标签:length   tin   nali   follow   find   int   logs   str1   size   

原文地址:http://www.cnblogs.com/hhkobeww/p/7672896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!