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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在引用中的代码段中,给定了一个整数数组。这段代码通过遍历数组中的元素,找到每个元素的下一个更大的数,并将其存储在数组中。具体的实现是通过维护一个栈来存储数组元素的下标。当遍历到一个元素时,如果栈不为空且当前元素大于栈顶元素所代表的元素,则将栈顶元素的下标出栈,并将其对应的数组元素赋值为当前元素。这样可以找到每个元素的下一个更大的数。如果栈为空或者栈顶元素所代表的元素大于当前元素,则将当前元素的下标入栈。最后,对于找不到下一个更大数的元素,将其对应的数组元素赋值为-1。通过这个算法,我们得到了以下结果:。这就是给定数组中每个元素的下一个更大的数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [找出数组中下一个大数](https://blog.csdn.net/f81892461/article/details/8681986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [phoenix upsert ERROR:MutationState size is bigger than maximum allowed number of bytes](https://blog.csdn.net/u012551524/article/details/81773851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值