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
解题思路:一开始便用了模拟来做,写起来还是比较舒服的,但是写完花时间把bug全找完以后交上去直接超时。。。。因为本题的数据是1e5,所以如果每次找到相等的数就要遍历一次的话必然会超时。
百度之后学到了memcmp函数,用此函数直接进行比较而不是去遍历的话就不会超时。
memcmp(const void *str1, const void *str2, size_t n)是把存储区 str1 和存储区str2 的前n个字节进行比较。该函数是按字节比较的,位于string.h。
str1-- 指向内存块的指针。
str2-- 指向内存块的指针。
n-- 要被比较的字节数。
返回值
如果返回值 < 0,则表示 str1 小于 str2。
如果返回值 > 0,则表示 str2 小于 str1。
如果返回值 = 0,则表示 str1 等于 str2。
!!memcmp函数和strcmp函数的区别:memcmp函数是按字节进行对比,而strcmp函数是按字符进行对比。!!
AC代码:
#include<stdio.h>
#include<string.h>
int a[100001],b[100001],c[300001];
int main() {
int T;
scanf("%d", &T);
int kns=1;
while(T--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int N,M;
int num1=0,num2=0;
scanf("%d%d", &N, &M);
for(int i=0;i<N;i++)
{
scanf("%d", &a[i]);
}
for(int i=0;i<M;i++)
{
scanf("%d", &b[i]);
}
int l=0;
int x=0,y=0;
while(x<N&&y<M)
{
if(a[x]>b[y])
{
c[l++]=a[x];
x++;
}
else if(a[x]<b[y])
{
c[l++]=b[y];
y++;
}
else
{
int f=memcmp(a+x,b+y,sizeof(a)); //此处是从相等的那一位(a+x和b+y的意思就是将开始比较的地方推到第一次相等的地方)开始,继续开始往后比,直到比出结果。
if(f<0)
{
while(a[x]==b[y]&&x<N&&y<M) //保证不越界
{
c[l++]=b[y];
y++;
}
}
else
{
while(a[x]==b[y]&&x<N&&y<M)
{
c[l++]=a[x];
x++;
}
}
}
}
while(x<N) //将剩下的同一个数组里的数给补上。
c[l++]=a[x++];
while(y<M)
c[l++]=b[y++];
printf("Case %d: ",kns);
for(int i=0;i<l;i++)
printf("%d",c[i]);
printf("\n");
kns++;
}
return 0;
}