Pat a problem-1038. Recover the smallest number (30)-sort/greedy, custom CMP function powerful AH!!!

Source: Internet
Author: User

Bo Master Welcome reprint, but please give the article link, I respect you, you respect me, thank you ~
Http://www.cnblogs.com/chenxiwenruo/p/6789138.html
Especially do not like those who reprint other people's original articles and do not give the link
So don't secretly copy blogger's blog Oh ~ ~

Test instructions: Given the number of N, the number of the smallest value after stitching.

From the beginning, simply put all the strings first from small to large sequence, and then splicing together to remove leading zeros,
It turns out there's a problem, like the following two
32 32321
If you compare by regular string, 32 is in front of 32321.
However, 32321-32 is the smaller solution.
How to have a good comparison method?
Makes 32>32321
Repeated padding is used until 8 bits are filled, because each number in the topic is up to 8 bits
Upcoming 32->32 (323232)
321->321 (32132)
In this case, the front is greater than the back. Output when the original can be output

#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>#include<cmath>#include<map>#include<vector>using namespacestd;Const intmaxn=10005;structnode{intLen; Charstr1[Ten]; Charstr2[ -]; BOOL operator< (ConstNode tmp)Const{}}NODE[MAXN],RNODE[MAXN];BOOLcmpChar*STR1,Char*str2) {    if(strcmp (STR1,STR2) <=0)        return true; Else        return false;}BOOLCmpnode (Node a,node b) {returnCMP (A.STR2,B.STR2);}intMain () {intN; scanf ("%d",&N);  for(intI=0; i<n;i++) {scanf ("%s", NODE[I].STR1); Node[i].len=strlen (NODE[I].STR1);  for(intj=0; j<=8/node[i].len;j++) {strcpy (node[i].str2+j*node[i].len,node[i].str1); } node[i].str2[8]=' /'; } sort (Node,node+n,cmpnode);//for (int i=0;i<n;i++) {//printf ("%s", NODE[I].STR2);//}//printf ("\ n");    BOOLleadzero=false; //in turn is the smallest number that is asked     for(intI=0; i<n;i++){        if(!Leadzero) {             for(intj=0; j<node[i].len;j++){                if(node[i].str1[j]!='0') {Leadzero=true; printf ("%s", node[i].str1+j);  Break; }            }        }        Else{printf ("%s", NODE[I].STR1); }    }    //if it's all 0,    if(!Leadzero) printf ("0\n"); return 0;}
View Code

There's a better solution to the problem. That's the custom sort, I return a+b<b+a
The idea is so wonderful that I have to say that the CMP function is powerful, and I'm limited by my thinking.
Source of ideas:
http://www.liuchuo.net/archives/2303

Pat a problem-1038. Recover the smallest number (30)-sort/greedy, custom CMP function powerful AH!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.