FZU - 2267 The Bigger the Better

 Problem 2267 The Bigger the Better

Accept: 118    Submit: 802
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

 Source

第七届福建省大学生程序设计竞赛-重现赛(感谢承办方闽江学院)


题意:

给出两组数,每次从其中一组的头部取出一个数放到目标数组的尾部。使目标数组最大。


思路:

如果两组数头部不相等时,取较大的即可;

如果相等,需要往后找到第一个不相等的元素,取较大的那个数组的头部。


代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
int n,m,a[200005],b[200005],ans[200005];
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int tt=1;tt<=cas;tt++)
    {
        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 pa=0,pb=0,cnt=0;
        while(pa<n && pb<m)
        {
            if(a[pa]>b[pb])
                ans[cnt++]=a[pa++];
            else if(a[pa]<b[pb])
                ans[cnt++]=b[pb++];
            else
            {
                int q=memcmp(a+pa,b+pb,sizeof(a));
                if(q<0)
                {
                    while(a[pa]==b[pb] && pa<n && pb<m)
                    {
                        ans[cnt++]=b[pb++];
                    }
                }
                else
                {
                    while(a[pa]==b[pb] && pa<n && pb<m)
                    {
                        ans[cnt++]=a[pa++];
                    }
                }
            }
        }
        while(pa<n)
            ans[cnt++]=a[pa++];
        while(pb<m)
            ans[cnt++]=b[pb++];
        printf("Case %d: ",tt);
        for(int i=0;i<cnt;i++)
            printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}
/*
1
4 4
1 2 3 4
1 2 5 6
*/

猜你喜欢

转载自blog.csdn.net/u013852115/article/details/80084651
今日推荐