CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法

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.

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.

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

题意:给你一些字符串,把字符串重新组合有很多种情况,把字典序最小的那种情况输出
思路:首先肯定是不能按照单个字符串的字典序排序然后从小到大输出,因为x的字典序肯定要比aa小,但是输出肯定是aax,这个时候只要用string,比较两两字符串联之后的字典序排序输出就行,stl真是强大。。。
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <string>
 6 using namespace std;
 7 struct node
 8 {
 9     string str;
10 
11 };
12 node str1[50010];
13 bool cmp(node a,node b)
14 {
15    return a.str+b.str<b.str+a.str;
16 }
17 int main()
18 {
19     int n;
20     while(scanf("%d",&n)!=EOF)
21     {
22         for(int i=1;i<=n;i++)
23         {
24             cin>>str1[i].str;
25         }
26         sort(str1+1,str1+1+n,cmp);
27         for(int i=1;i<=n;i++)
28            cout<<str1[i].str;
29         printf("\n");
30     }
31     return 0;
32 }
View Code

这个时候说一下string的用法

注意是#include <string>而不是#include <cstring>

a)    string s;  //生成一个空字符串s
b)    string s(str) //拷贝构造函数 生成str的复制品
c)    string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
d)    string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
e)    string s(cstr) //将C字符串作为s的初值
f)    string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。
g)    string s(num,c) //生成一个字符串,包含num个c字符
h)    string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值
i)    s.~string() //销毁所有字符,释放内存

2.字符串操作函数
a) =,assign()   //赋以新值
b) swap()   //交换两个字符串的内容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //删除字符
f) clear() //删除全部字符 
g) replace() //替换字符
h) + //串联字符串
i) ==,!=,<,<=,>,>=,compare()  //比较字符串
j) size(),length()  //返回字符数量
k) max_size() //返回字符的可能最大个数
l) empty()  //判断字符串是否为空
m) capacity() //返回重新分配之前的字符容量
n) reserve() //保留一定量内存以容纳一定数量的字符
o) [ ], at() //存取单一字符
p) >>,getline() //从stream读取某值
q) <<  //将谋值写入stream
r) copy() //将某值赋值为一个C_string
s) c_str() //将内容以C_string返回
t) data() //将内容以字符数组形式返回
u) substr() //返回某个子字符串
v)查找函数
w)begin() end() //提供类似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //返回配置器

posted @ 2016-05-02 10:19  as3asddd  阅读(400)  评论(0编辑  收藏  举报