Kia's Calculation(HDU 4267)

Problem Description
Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 1234+9876, she will get 0. Ghee is angry about this, and makes a hard problem for her to solve:
Now Kia has two integers A and B, she can shuffle the digits in each number as she like, but leading zeros are not allowed. That is to say, for A = 11024, she can rearrange the number as 10124, or 41102, or many other, but 02411 is not allowed.
After she shuffles A and B, she will add them together, in her own way. And what will be the maximum possible sum of A "+" B ?
 
Input
The rst line has a number T (T <= 25) , indicating the number of test cases.
For each test case there are two lines. First line has the number A, and the second line has the number B.
Both A and B will have same number of digits, which is no larger than 106, 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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int a[2000020],b[2000020];
char A[20],B[20];
int ans[2000020];

int main()
{
    int T,iCase = 0;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        iCase++;
        scanf("%s%s",A,B);
        int n = strlen(A);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = 0; i < n; i++)
        {
            a[A[i] - '0']++;
            b[B[i] - '0']++;
        }
        int x = 0, y = 0;
        int ttt = -1;
        //求出首位数字
        for(int i = 1; i <= 9; i++)
            for(int j = 1; j <= 9; j++)
                if(a[i] && b[j] && ((i+j)%10) > ttt )
                {
                    x = i;
                    y = j;
                    ttt = (x+y)%10;
                }
        a[x]--;
        b[y]--;
        int cnt = 0;
        ans[cnt++] = (x+y)%10;
        //求出其它位结果保存到ans数组中
        for(int p = 9; p >= 0; p--)
        {
            for(int i = 0; i <= 9; i++)
                if(a[i])
                {
                    if(i <= p)
                    {
                        int j = p-i;
                        int k = min(a[i],b[j]);
                        a[i] -= k;
                        b[j] -= k;
                        while(k--) ans[cnt++] = p;
                    }
                    int j = 10 + p - i;
                    if(j > 9)continue;
                    int k = min(a[i],b[j]);
                    a[i] -= k;
                    b[j] -= k;
                    while(k--) ans[cnt++] = p;
                }
        }
        printf("Case #%d: ",iCase);
        int s = 0;
        while(s < cnt-1 && ans[s] == 0)s++;
        for(int i = s; i < cnt; i++) printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}
贪心

 

 

posted @ 2013-09-12 19:32  1002liu  阅读(262)  评论(0编辑  收藏  举报