ACM Learning process-hdu 4726 Kia ' s calculation (greedy && counting sort)

Source: Internet
Author: User

Description
Doctor Ghee is teaching Kia how to calculate the sum of the integers. But Kia was so careless and alway forget to carry a number when the sum of of the digits exceeds 9. For example, when she calculates 4567+5789, she'll get 9246, and for 1234+9876, she'll get 0. Ghee is angry on this, and makes a hard problem for hers to solve:
Now Kia have integers A and B, she can shuffle the digits in each number as she is like, but leading zeros is not allowed . That's to say, for A = 11024, she can rearrange the number as 10124, or 41102, or many other, but 02411 was not allowed.
After she shuffles A and B, she'll add them together, in her own. And what'll be the maximum possible sum of A "+" B?

Input
The RST line has a number T (T <=), indicating the number of test cases.
For the test case there is lines. First line have the number A, and the second line have the number B.
Both A and B would have same number of digits, which is no larger than 6, and without leading zeros.

Output
For test case X, output ' case #X: ' First, then output the maximum possible sum without leading zeros.

Sample Input
1
5958
3036

Sample Output
Case #1:8984


The title requirement is given two numbers, which can be combined with another number, which requires the largest number, but the first digit cannot be synthesized by 0.
However the topic gives AB two number lengths to reach 10^6 (at first the condition was misunderstood, thought to be AB Upper bound is 10^6, then decisive violence timed out)
However, although the AB length reaches 10^6, each one is made up of 0 to 9 digits, and the addition of the topic is performed by each person. Consider counting the numbers from 0 to 9 and then go greedy.
Since the first digit cannot be combined with 0, the enumeration of the first addition is the most important.
Then it is greedy for the number of digits in the back:

For no more than 10 of the situation, naturally starting from 9 greedy, and then 8, 7, 6 ...
And because of the addition of the case of K, each can be added to the k is different, nature does not affect each other, so for each can be added to the case of K, all the pairs are exhausted, until a group and Group B is reduced to 0.

For the case where mode 10 is required. If the same mold 10 plus the synthesis of k, then must first consider the modulus 10 to get K, will be considered not to get k-1, so consider the situation of non-mold immediately consider the situation of the mold.

In addition, there is a point to note that A and B in a group of only 0 or two groups are only 0 of the situation, the need for special sentences.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;inta[ the], b[ the], ans[1000010];voidInput () {memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); Charch;  for (;;) {CH=GetChar (); if(ch = ='\ n')             Break; A[ch-'0']++; }     for (;;) {CH=GetChar (); if(ch = ='\ n')             Break; B[ch-'0']++; }}voidWork () {intCNT =0, Maxone =-1, Ione, Jone;  for(inti =1; I <Ten; ++i) {if(A[i] = =0)            Continue;  for(intj =1; J <Ten; ++j) {if(B[j] = =0)                Continue; if(Maxone < (i+j)%Ten) {Maxone= (i+j)%Ten; Ione=i; Jone=J; }        }    }    if(Maxone! =-1)//the first digit is 0 plus one number.{ans[cnt]=Maxone; CNT++; A[ione]--; B[jone]--; }     for(intK =9; K >=0; k--)    {         for(inti = k; I >=0; --i) { while(A[i] && b[k-i]) {ans[cnt]=K; CNT++; A[i]--; B[k-i]--; }        }         for(inti = k; I <Ten; ++i) { while(A[i] && b[k+Ten-i]) {ans[cnt]=K; CNT++; A[i]--; B[k+Ten-i]--; }        }    }    inti =0;  while(Ans[i] = =0&& I < CNT)//eliminate the first occurrence of the 0 situationi++; if(i = = cnt)//all the bits are in the 0 case.{printf ("0\n"); return; }     for(; i < CNT; + +)i) printf ("%d", Ans[i]); printf ("\ n");}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);    GetChar ();  for(intTimes =1; Times <= T; ++Times ) {printf ("Case #%d:", times);        Input ();    Work (); }    return 0;}

ACM Learning process-hdu 4726 Kia ' s calculation (greedy && counting sort)

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.