1198. Substring Constraints
Time limit:1 secs, Memory limit:32 MB
Description
Dr Lee cuts a string S into N pieces,s[1],..., s[n].
Now , Dr. Lee gives you these N sub-strings:s[1],... s[n]. There might be several possibilities, the string S could be. For example, if Dr. Lee gives you three sub-strings {"A", "AB", "AC"}, the string S could is "Aabac", "Aacab", "ABAAC",...
Your task is to output the lexicographically smallest S.
Input
The first line of the input was a positive integer T. T is the number of the the test cases followed.
The first line of all test case is a positive integer N (1 <=n<= 8) which represents the number of sub-strings. After that, N lines followed. The i-th line is the i-th sub-string s[i]. Assume the length of each sub-string are positive and less than 100.
Output
The output of each test is the lexicographically smallest S. No redundant spaces is needed.
Sample Input
13aabac
Sample Output
Aabac
Problem Source
ZSUACM Team Member
The idea of this problem is relatively simple, the topic each given n strings, and then asked to output the string with the N-string combination of the smallest dictionary, the string needs to be used all and can not be reused.
First we need to know how to determine the composition of the smallest sequence of dictionaries, by the string A and B, if the a+b is less than b+a, then we choose the b+a combination, the other is to choose the a+b combination, based on such a judgement, we can directly define a CMP to all the strings sorted by sort;
In CMP (const string &x1,const string &x2), we only need to return to x1+x2<x2+x1, then we finally get the sort output in order, that is, the minimum dictionary order required by the title string.
The code is as follows:
#include <iostream> #include <algorithm>using namespace std;string s[16];bool cmp (const string &X1, Const string &x2) { return x1+x2<x2+x1; } int main () {int T;cin>>t;while (t--) {int n;cin>>n;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];} Cout<<endl;} return 0;}
Sicily 1198 Substring