zoukankan      html  css  js  c++  java
  • pat 1038 Smallest Number解题心得

    题目描述:

    1038. Recover the Smallest Number (30)

    Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

    Input Specification:

    Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the smallest number in one line. Do not output leading zeros.

    Sample Input:
    5 32 321 3214 0229 87
    
    Sample Output:
    22932132143287


    其实初看这个题的时候没有觉得太难,第一时间的想法是涉及到的肯定要字典排序,然后进行比对。
    不过问题的关键在于出现类似示例中2.3.4几个数据,存在前边是后边的字串的问题,这种情况下,单纯的排序或是直接比较都是不行的,经过观察我们发现子串与父串的连接顺序的决定因素在于父串的多余结尾与子串开头部分的大小相比,因此,网上的一些人采取的是,将串扩充至最大位(按照重复原有串的原则),然后进行字典排序,再截取对应的;这种方法肯定是可以,不过会使空间浪费很多,而且也造成排序的比较过多。
    经过观察,我们发现排序的要素综合考虑字典排序和子串因素,可以归为str1+str2<str2+str1的判断,用得sort多的人就能看出来这是在考虑sort的cmp条件,基于这些,这里的问题就可以解决了,为了节约时间,对开头不同的元素分别装在不同的容器中,对于排序来说就会省时多了,因为规模减小了,下边把代码贴出来,供大家参考和讨论,有可以改进的欢迎讨论
    当然,特殊情况也不能忽略了哦,就是全是0串的时候,记得输出0~~
     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <stdlib.h>
     6 using namespace std;
     7 vector<string> num[10];
     8 bool cmp(string s1, string s2){
     9     return s1 + s2 < s2 + s1;
    10 }
    11 int main(){
    12     string temp,tempI,result="";
    13     int N, i, j, k,flag=0;//flag shows the state whether the vector is modified or not
    14     vector<string>::iterator vI;
    15     cin >> N;
    16     for(i = 0; i < N; i++){
    17         cin >> temp;
    18         num[temp[0] - '0'].push_back(temp);
    19     }
    20     for (i = 0; i < 10; i++){
    21         if (num[i].size() != 0)
    22             sort(num[i].begin(), num[i].end(), cmp);
    23         for (vI = num[i].begin(); vI != num[i].end(); vI++){
    24             result = result + *vI;
    25         }
    26     }
    27     for (i = 0; i < result.size(); i++){
    28         if (result[i] != '0')
    29             break;
    30     }
    31     if (i == result.size())
    32         printf("0");
    33     else
    34         cout << result.substr(i,result.size());
    35     system("pause");
    36     return 0;
    37 }

    另外,虽说这里因为想用容器就用的C++,其实可以用C的地方尽量用C,例如输入输出等,博主曾经吃过很多亏,好啦,纪念第一天开通博客~~~撒花~~~~~

  • 相关阅读:
    谈URL中末尾斜杠对SEO的影响
    ORDER BY一个较高级的用法
    MYSQL5.5 提示 Mysq error:Cannot load from mysql.proc
    mysql 数据库信息泄露
    [转]PclZip简介与使用
    通过telnet命令查看memcache运行状态
    [转载]PHP上传问题总结(文件大小检测,大文件上传)
    Silex 基于Symfony2组件的微型框架
    [转]推荐一些不错的计算机书籍
    [转]Beanstalkd简介(job生命周期)
  • 原文地址:https://www.cnblogs.com/KarayLee/p/KarayLee_1038PAT.html
Copyright © 2011-2022 走看看