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
题意:输入一个数t,有t组数据,输入n,m表示下面有两行 一行有n数,一行m 的数,两个数列 从前往后比较,大的输出,组成一个最大的数;

思路:模拟,要是单纯的模拟,一定超时,因为比较时,有相等的情况,本来想想用 dp[][],记忆一下,但是 两个数列,太长了100000, 但是你也不能已经比较过的,再比较一边,这不就重复比较了吗,怎么能利用 已知比较过的答案呢?

像这里这种情况,就能很好的比较重复;

2 2 2 5

2 2 2 4

要是你 单纯的模拟 ,遇到相同找后面不相等时 数那个大,你会比较3次(有几次个连续相等就会比较几次),所以呢,你怎样能避免重复比较呢? 因为当你第一次比较时,就知道第个数列大了,当你把第一数列的2输出时,你有把第一个数列第二个2和第二个数列的第一个2比较,这时又相等,你又找不相等时,谁最大,找到最后还是,数列一最大,输出数列一第二个2(第二次比较是不是做的无用功)

这样就避免的那种情况:

 

	else if(a[i]==b[j])
			{
				int k = strcmp(a+i,b+j); 
				if(k>0)
				{
					while(i<n&&a[i]==b[j])   // 想这种情况:2 2 2 5避免多次比较;
					{                        //             2 2 2 4 
						printf("%c",a[i]);
						i++;
					}
				
				}
				else 
				{
					while(j < m&&a[i] == b[j])
					{
						printf("%c",b[j]);
						j++;
					} 
				}
			}

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100010

//int a1[Max],b1[Max];
char a[Max],b[Max];
int main()
{
	int i,j,t;
	int n,m;
	scanf("%d",&t);
	int num = 1;
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int x;
		for(i =0 ;i<n;i++)
		{
			scanf("%d",&x);
			a[i] = x+'0';
		}
		a[i] = '\0';
		for(i = 0;i<m;i++)
		{
			scanf("%d",&x);
			b[i] = x+'0';
		}
		b[i] = '\0';
		i = 0;j = 0;
		printf("Case %d: ",num++);
		while(i<n&&j<m)
		{
			if(a[i]>b[j])
			{
				printf("%c",a[i]);
				i++;
			}
			else if(a[i]==b[j])
			{
				int k = strcmp(a+i,b+j); 
				if(k>0)
				{
					while(i<n&&a[i]==b[j])   // 想这种情况:2 2 2 5避免多次比较;
					{                        //             2 2 2 4 
						printf("%c",a[i]);
						i++;
					}
				
				}
				else 
				{
					while(j < m&&a[i] == b[j])
					{
						printf("%c",b[j]);
						j++;
					} 
				}
			}
			else 
			{
				printf("%c",b[j]);
				j++;
			}
		}
		while(i<n)
		{
			printf("%c",a[i]);
			i++;
		}
		while(j<m)
		{
			printf("%c",b[j]);
			j++;
		}
		printf("\n");
	}
	return 0;
	
 } 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值