Description
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.< /c8>
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 中文版 letters. The sum of string lengths would not exceed 5 104.
Output
Print the only String a -the lexicographically smallest string concatenation.
Sample Input
Input
4
Abba
Abacaba
Bcd
Er
Output
Abacabaabbabcder
Input
5
X
Xx
Xxa
Xxaa
Xxaaa
Output
Xxaaaxxaaxxaxxx
Input
3
C
Cb
Cba
Output
Cbacbc
Test instructions: give you some strings, put strings back together in a number of cases, the smallest dictionary order of the situation output
Idea: First of all, it must not be sorted by the dictionary order of a single string and then from small to large output, because the dictionary order of x is definitely smaller than AA, but the output is definitely AAX, this time as long as String, compare 22 characters in series after the dictionary ordered output line, STL is really powerful ...
1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <algorithm>5#include <string>6 using namespacestd;7 structnode8 {9 stringstr;Ten One }; ANode str1[50010]; - BOOLCMP (node A,node b) - { the returna.str+b.str<b.str+A.str; - } - intMain () - { + intN; - while(SCANF ("%d", &n)! =EOF) + { A for(intI=1; i<=n;i++) at { -Cin>>Str1[i].str; - } -Sort (str1+1, str1+1+n,cmp); - for(intI=1; i<=n;i++) -cout<<Str1[i].str; inprintf"\ n"); - } to return 0; +}
View Code
Let's talk about the use of string at this time
Note is # include <string> not # include <cstring>
A) string s; //generates an empty string s
B) string s (str)//Copy constructor generates a copy of STR
C) string s (str,stridx)// Use the section "starting at position Stridx" in the string str as the initial value of the string
D) string s (str,stridx,strlen)//String in str "starting at Stridx and length at most strlen" Part as the initial value of the string
E) string s (cstr)//C string as the initial value of S
F) string s (chars,chars_len)//C string before chars _len characters as the initial value of a string s.
G) string s (num,c)//Generate a string containing num C characters
H) string s (beg,end)//with interval beg;end (does not contain end) The characters within the string s as the initial value of
I) s.~string ()//Destroy all characters, free memory
2. String manipulation functions
A) =,assign ()//Assign new value
b) Swap ()//Exchange two strings of content
c) +=,append (), push_back ()//Add characters at the tail
d) Insert ()//Insert character
e) Erase ()//delete character
f) Clear ()//delete all characters
g) Replace ()//Replace character
h) +//concatenation string
i) ==,!=,<,<=,>,>=,compare ()//Compare strings
j) Size (), Length ()//number of characters returned
k) max_size ()//The maximum possible number of characters returned
L) empty ()//Determine if the string is empty
m) Capacity ()//return the character capacity before reallocation
N) reserve ()//retain a certain amount of memory to accommodate a certain number of characters
o) [], at ()//Access single character
p) >>,getline ()//read a value from stream
Q) <<//write value to stream
r) Copy ()//Assign a value to a c_string
s) c_str ()//return content as C_string
T) data ()//returns the content as a character array
u) substr ()//return a substring
V) Find function
W) begin () end ()//provide an STL-like iterator support
x) Rbegin () rend ()//Reverse iterator
Y) Get_allocator ()//Return Configurator
Codeforces 632C the smallest string concatenation//with string and sort is good &&string basic usage