SGU[347] Join the Strings

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.

Berland Berl XV的国王殿下是一个非常聪明的人,并且有一个才艺高超的妻子。她意识到一个事实——只有曾经具有非常突出的人格魅力的人才能在辉煌的历史上写下他们的名字,并且名垂青史。Berland Berl XV的国王殿下曾经一直拥有深切和真挚的尊重以及他妻子的信任,但是现在都失去了。于是他决定编写一部自己的编年史。由于误解的模糊性以及有歧义且不公正的历史,他决定将他所有的皇家职责都抛之脑后并且决定编写编年史,这会使得他变得著名,真实且荣耀的描绘他所有的英雄事迹。

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!

国王殿下组织了他的国家的最大的头脑大会——学术编年史大会(Academic Chroniclers Meeting——ACM),他这样来命名它。并且决定测试与会人员。任务是使用N个给定的字符串建造最小字典序的拼接(SLC)。N个字符串的SLC具有最小的字典序,且拼接Si1, ..., SiN中的i1, ..., iN是从1到N的一个排列。称为一个编年史者是非常荣耀的事情,因此不要错过机会也不要把它搞砸了。使得王国选择你做为编年史者。 

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.

第一行包含一个整数N (1 <= N <= 100),代表字符串的个数。接下来N行包含N个字符串,每行一个字符串。字符串的长度不超过100.每个字符串只包含小写拉丁字母,没有行首或行尾空格。


Output

输出

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

在输出文件上输出一行,表示N个给定的字符串的SLC。


Sample Input

样例输入

6

it

looks

lilke

an

easy

problem


Sample Output

样例输出

aneasyitlikelooksproblem

 

Analysis

分析

一开始没看题意,只看了输入输出,以为是按字典序连接后输出,后来才发现是使得连接后的字典序最小。

既然要使得连接后的字典序列最小,那么对于任意两个相邻的字符串x, y当且仅当x + y的字典序比y + x的字典序大的时候交换,因此直接排序即可。

对于直接按字典序最小排序输出的一个很好的反例是:对于两个字符串ac和aca,应该输出acaac,而不是acaca。

 

Solution

解决方案

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

const int MAX = 128;

string pData[MAX];

int cmp(string x, string y)
{ return x + y < y + x; }

int main()
{
	int N;
	while(cin >> N)
	{
		for(int i = 1; i <= N; i++)
		{ cin >> pData[i]; }
		sort(pData + 1, pData + N + 1, cmp);
		for(int i = 1; i <= N; i++)
		{ cout << pData[i]; }
		cout << endl;
	}
	return 0;
}

 

这道题目总的来说不难,但是要把总的字典序最小转换到排序的问题上,本身还是具有一定难度的。

而且以后一定要认真看题,不能直接看输入输出。

不过觉得这道题目好像还在考英语。

原文地址:https://www.cnblogs.com/Ivy-End/p/4297429.html