(FZU 2267)The Bigger the Better (贪心,考虑清楚就行)

                                                                  Problem 2267 The Bigger the Better

                                                Accept: 129    Submit: 865
                            Time Limit: 1500 mSec    Memory Limit : 32768 KB

                                             Problem Description

Fat brother and Maze are playing a kind of special (hentai) game with two integers. All the digits of these two integers are in the range from 1 to 9. After they’ve got these two integers, they thought that these two numbers were not large enough! (You know that the young people always like the HUGE THING in order to have more pleasure) So they decide to use the digits of these two integers to make a new BIGGER integer. At the beginning of this game, the new integer has no digit, each time Fat Brother and Maze can choose one of the two initial integers (if this integer exists) and move its first digit to the end of the new integer. For instance, if the new integer is 233 and the two initial integers are 3154 and 1324 now, they can choose the first digit of the integer 3154, which is 3, and add it to the end of the new integer to make it become 2333. The two initial integers are 154 and 1324 now after this action. Also they can choose the first digit of the integer 1324 and add it to the end of the integer 233 and make it become 2331. This process goes until the two initial integers are all empty. Now Fat Brother and Maze would like to know the maximum number they could get after this special (hentai) game.

Input

The first line of the date is an integer T (1 <= T <= 102), which is the number of the text cases.

Then T cases follow, each case contains two integers N and M (1 <= N,M <= 100000) indicated the number of the digits of these two integers. Then a line with N digits indicates the first integer and a line with M digits indicates the second integer. Note that all the digits are in the range from 1 to 9.

In 90% of test cases, N, M <= 1000.

Output

For each case, output the case number first, and then output the integer Fat Brother and Maze would get, this integer should be as large as possible.

Sample Input

13 42 5 23 6 3 1

Sample Output

Case 1: 3632521

#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#define maxn 200005
using namespace std;
int a[maxn], b[maxn];

int main(){
    int n, m;
    int t;
    int cas = 1;
    cin >> t;
    while(t--){
        scanf("%d%d", &n, &m);
        memset(a, 0, sizeof(a));   //这一步必须要有,因为是后面要用到比较函数,那个按照字节比较非常有毒的函数.
        memset(b, 0, sizeof(b));
        for(int i = 0; i < n; i++){
            scanf("%d", &a[i]);
        }
        for(int i = 0; i < m; i++){
            scanf("%d", &b[i]);
        }
        int i = 0, j = 0;
        printf("Case %d: ", cas++);
        while(i < n && j < m){
            if(a[i] > b[j]){
                printf("%d", a[i++]);
            }
            else if(a[i] < b[j])
                printf("%d", b[j++]);
            else{
                int q = memcmp(a+i, b+j, sizeof(a));
                if(q < 0){
                    while(a[i] == b[j] && j < m){
                        printf("%d", b[j++]);
                    }
                }
                else{
                    while(a[i] == b[j] && i < n){
                        printf("%d", a[i++]);
                    }
                }
            }
        }
        while(i < n){
            printf("%d", a[i++]);
        }
        while(j < m){
            printf("%d", b[j++]);
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值