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.
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.
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.
1 3 4 2 5 2 3 6 3 1Sample Output
Case 1: 3632521
题意:给出n和m,第一个数列a有n个数,第二个数列b有m个数。每次从a或b最左端中取出一个数直到n+m个数取完,问能组成的最大的数是多少。
解题思路:当然一上来就想到模拟啦
应该注意的是当两个数列当前最左边的数相等时该怎样判断。
思路就是不断的往后找,直到找到两个不同的数,如果那个数a的b比大就把a中那个相同的数取出来,反之取b中的。
举个例子:
a: 1 2 5 6
b: 1 2 7 8
最左端的数:
第一轮比较:
1 相等
往后找,2 相等,5<7所以取b数列的1
第二轮比较:
1<2 取b数列的2
第三轮比较:
1<7 取b数列的7
第四轮比较:
1<8 取b数列的8
第五轮比较:
(比较个P比较完了)
b数列中的数已全部取出,即lb以及到了m,直接把a数列中剩余的依次取出即可。
所以结果是
12781256
想归这么想,可是写起来超时了= =(果真是没脑子)
注意到数据是100000,用for循环对每次相等的情况进行判断势必会超时,这时又得到了大佬kangzzz的指点(双击666),
可以使用memcmp函数直接进行比较而不会超时!
关于memcpy:memcpy百度百科
memcmp是比较内存区域buf1和buf2的前count个字节。该函数是按字节比较的。
当然也看到很多人是用后缀数组写的,后缀数组是什么到现在还一头雾水,慢慢学习吧Orz
AC代码:
#include<stdio.h>
#include<string.h>
int a[200010],b[200010],ans[200010];
int main()
{
int t,tt=1,n,m;
scanf("%d",&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 la=0,lb=0,cnt=0;
while(la<n&&lb<m)
{
if(a[la]>b[lb]) ans[cnt++]=a[la++];//如果a的第一位比b的第一位大
else if(a[la]<b[lb]) ans[cnt++]=b[lb++];//如果a的第一位比b的第一位小
else//如果a和b的第一位相等
{
int f=memcmp(a+la,b+lb,sizeof(a));//比较从相等开始到最后的子串的值,谁大加谁
if(f<0)
{
while(a[la]==b[lb]&&la<n&&lb<m)//b大加b
{
ans[cnt++]=b[lb++];
}
}
else
{
while(a[la]==b[lb]&&la<n&&lb<m)//a大加a
{
ans[cnt++]=a[la++];
}
}
}
}
//不要忘记把剩余的加上
while(la<n) ans[cnt++]=a[la++];
while(lb<m) ans[cnt++]=b[lb++];
printf("Case %d: ",tt++);
for(int i=0;i<cnt;i++)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}
/*
1
4 4
1 2 5 6
1 2 7 8
*/