[2016-03-26][codeforces][632][C][The Smallest String Concatenation]

  • 时间:2016-03-26 10:32:13 星期六

  • 题目编号:[2016-03-26][codeforces][632][C][The Smallest String Concatenation]

  • 题目大意:给定n个字符串,求把n个字符串拼接起来,使得字符串的字典序最小,输出最后答案

  • 分析:直接排序

    • 排序cmp方法:直接比较 a+b 和 b+a的字典序,小的一定在前面
  1. #include <string>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <iostream>
  6. using namespace std;
  7. typedef long long LL;
  8. const int maxn = 5 * 1E4 +10;
  9. char str[maxn][55];
  10. char* pstr[maxn];
  11. bool cmp(string str1,string str2){
  12. string tmp = str1;
  13. str1 += str2;
  14. str2 += tmp;
  15. return str1 < str2;
  16. }
  17. int main(){
  18. int n;
  19. scanf("%d",&n);
  20. getchar();
  21. for(int i = 0;i < n ; ++i){
  22. gets(str[i]);
  23. pstr[i] = str[i];
  24. }
  25. sort(pstr,pstr + n,cmp);
  26. for(int i = 0;i < n ; ++i){
  27. cout<<pstr[i];
  28. }
  29. cout<<'\n';
  30. return 0;
  31. }


来自为知笔记(Wiz)


posted on 2016-03-26 11:17  红洋  阅读(108)  评论(0)    收藏  举报

导航