MINSEQ - Minimal Possible String

Given two strings A and B, your are to find the lexicographically smallest string after inserting B into A.

For example, string A is "369", and string B is "4799". There are 4 ways that I can insert B into A, and I’ll get 4 different results: 4799369, 3479969, 3647999, 3694799. Thus, 3479969 is the lexicographically smallest one.

Input

Input contains several cases. Each case has 2 strings A and B with length no longer than 100000 in 2 lines. Process the input until EOF. The strings consist of only digit 1-9.

Output

For each case, output the minimal possible result.

Example

Input:
369
4799
666
12345

Output:
3479969
12345666

Added by:3xian
Date:2009-10-30
Time limit:1s-1.567s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 PERL6
Resource:GDUT Monthly

hide comments
2021-04-21 00:23:02 Scape
Wonderful problem. Also solvable with hashing.
2015-07-28 11:38:05 Abhishek pratap singh
I am using Suffix Array to rank the suffixes but no getting WA.
I have tried these test cases
987
9876
8769
9876
657
6567

Last one was the most tricky one that I could think of :/

Can anyone help me?
2014-12-24 22:08:22 paras meena
are u kidding my O((|A|+|B|)*(log(|A|+|B|)^2)) gave TLE o.O and by DC3 i got WA what the hack :/
2014-05-29 12:29:38 ABHISHEK
Can anybody give a hint as to how to approach this problem.
2012-09-21 05:05:49 Ravi Kiran
Really Nice Problem and Good test data!
2012-09-15 00:22:10 Buda IM (retired)
O( |A|log(|A|+|B|) ) is enough to get AC
2012-06-16 20:50:00 :D
For now I have TLE with O(len(A)+len(B)) so I doubt anything less with give AC.

EDIT: My bad. Due to bad algo, complexity was much higher. Got 0.41 with actual O(len(A)+len(B)) (some I/O optimization included).

Last edit: 2012-11-24 21:19:14
2011-07-14 13:24:01 Varun Kumar V
is this problem solvable in AlogA + B ?? Or even better solution is required ???
2014-01-27 21:10:51 Brainstorm
worse problem explanation ever.
I did understand what is written, and I'm quite sure my code works, still, it tells me the 'wrong answer' error.
Are we supposed to guess hidden rules?
-------
3xian: Sometimes python would crash on SPOJ when using large memory. But your submissions which got 'wrong answer' are really wrong, please check them carefully.

Last edit: 2012-06-03 09:47:15
2011-06-26 16:54:23 3xian
@Xilinx
It is caused by ASCII 13 and 10, but not redundant empty lines.
Anyway, “scanf()” is recommended.

Last edit: 2010-02-12 08:58:59
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.