The Bigger the Better

FZU - 2267 

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

1
3 4
2 5 2
3 6 3 1

Sample Output

Case 1: 3632521

题目大意:输入两个数列,两个数列的每个位置进行比较,先输出大的数,必须得先输进去的数输出来之后 后输进去的数才能输出来,当两个数相同时,继续往后看,直到出现不相同的数时输出较大的序列。

用到了memcmp, /比较函数,是一种按照字节比较的函数 

代码如下:

#include <iostream>   
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
int a[MAXN],b[MAXN];
int main(){
	int T;
	int n,m;
	cin>>T;
	int k=0;
	while(T--){
		k++;
		memset(a, 0, sizeof(a));   
        memset(b, 0, sizeof(b));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);  //输入a数组 
		for(int i=1;i<=m;i++) scanf("%d",&b[i]);  //输入b数组
		printf("Case %d: ",k);
		int i=1,j=1;
		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]&&i<=n)	printf("%d",a[i++]);
				}
				else{
					while(a[i]==b[j]&&j<=m) printf("%d",b[j++]);
				}
			}	
		}
		while(i<=n) printf("%d",a[i++]);
		while(j<=m) printf("%d",b[j++]);
		printf("\n"); 
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42759455/article/details/82054444