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!!!