CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • May Cook-Off
    • May Long 2012
    • April Cook-Off
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » April 2010 Contest » The Reversed World

The Reversed World

Problem code: REVERSED

  • All Submissions

All submissions for this problem are available.

Given three numbers A, B and C all having N digits, little Johnny needs to write a program to permute the digits of the number C so that the new number is greater than both A and B.

This problem would be so easy if Johnny's teacher didn't require that Johnny's program must be correct in both the real world and the reversed world. What happens once the world is reversed? One thing Johnny knows for sure is that all the numbers are also reversed. The second thing, the "greater than" relation becomes the "smaller than" relation.

For the sake of clarity, we will use aR to denote the number a in the reversed world (for instance, 113R = 311).

Thus Johnny task is now permute the digits of C so that the new number is greater than both A and B. In additional, the reverse of the new number should be smaller than both AR and BR. If there are multiple possible solutions, Johnny needs to find the smallest one.

As usual, please help Little Johnny with his task!

Input

The first line contains a number T (about 10) which is the number of the test cases. Each test case has the following form.

The first line contains N (1 <= N <= 1000000).

The second line contains the number A.

The third line contains the number B.

The fourth line contains the number C.

Each test case's input is separated by a blank line.

Output

For each test case, print the solution in a single line. If there is no solution, print the number -1.

Example

Input:
2

2
12
23
32

3
124
324
335

Output:
-1
353


Date: 2010-03-11
Time limit: 3s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK JS


  • Submit

Comments

  • Login or Register to post a comment.

@admin what is N here????

dabbcomputers @ 2 Apr 2010 04:02 PM

@admin

what is N here????

If you read the problem

walrusvn @ 2 Apr 2010 04:07 PM

If you read the problem carefully, note that the first line says "Given three numbers A, B and C all having N digits"  : )

" Johnny needs to find the

cgy4ever @ 2 Apr 2010 05:18 PM

" Johnny needs to find the smallest one."

Is that  means smallest in the "real world"?

Yes.

i0exception @ 2 Apr 2010 05:54 PM

Yes.

Can the number begin or end

cedricl @ 4 Apr 2010 12:08 AM

Can the number begin or end in 0?

same old problem of TLE!

amolbhave @ 4 Apr 2010 09:39 AM

same old problem of TLE!

Are the values of A and B

amolbhave @ 4 Apr 2010 10:18 AM

Are the values of A and B unique? Can they be ever same?

A and B can be any N digit

triplem @ 4 Apr 2010 10:20 AM

A and B can be any N digit numbers. There aren't any other restrictions placed on them in the problem statement.

sorry I mean values of A, B

amolbhave @ 4 Apr 2010 10:28 AM

sorry I mean values of A, B and C

Well clearly there aren't any

triplem @ 4 Apr 2010 10:51 AM

Well clearly there aren't any restrictions on C either..

Yes, there's no restriction.

writerAPR10 @ 4 Apr 2010 11:02 AM

Yes, there's no restriction. Numbers can begin or end with 0.

wts TLE?

urbaddy @ 4 Apr 2010 03:54 PM

wts TLE?

TLE means Time limit

writerAPR10 @ 4 Apr 2010 08:07 PM

TLE means Time limit Exceeded.

can the new C be equal to

edog @ 5 Apr 2010 01:26 AM

can the new C be equal to C??

for example, in the sample i/o, 335 IS greater than 124 and 324 and less than 421 and 423!

The sample output is correct.

triplem @ 5 Apr 2010 02:31 AM

The sample output is correct. You have misunderstood the problem, read it again.

I asserted for testcase<=15

rockaustin2k6 @ 5 Apr 2010 03:05 AM

I asserted for testcase<=15 and still the assert is false ..So may I am wrong or test cases are > 15

Is number of test cases > 15 ?

There is no exact limit on

akuegel @ 5 Apr 2010 11:53 AM

There is no exact limit on the number of test cases given here "(about 10)". So I guess it means that on average there are 10 test cases, but some files may contain more than 10 (and even more than 15).

Yes, the average of the

writerAPR10 @ 6 Apr 2010 12:15 AM

Yes, the average of the number of test cases is about 10, but there's one file that have more than 15 test cases : )

"greater than relation

ravi_villuri @ 7 Apr 2010 10:30 AM

"greater than relation becomes the smaller than relation". What does this mean? Does it mean 5>6 in the reversed world?

It just means the reverse of

triplem @ 7 Apr 2010 10:34 AM

It just means the reverse of your output must be smaller than the reverse of the input numbers (as opposed to greater than).

Thanks Stephen, that sentence

ravi_villuri @ 7 Apr 2010 04:40 PM

Thanks Stephen, that sentence puzzled me a bit. Finally got it working.

The output should be -1 if

ikon_me @ 9 Apr 2010 06:50 PM

The output should be -1 if A,B,C all are same, isn't it?

The output should be -1 if

ikon_me @ 9 Apr 2010 06:50 PM

The output should be -1 if A,B,C all are same, isn't it?

Hey , the output should be -1

ikon_me @ 9 Apr 2010 06:50 PM

Hey , the output should be -1 if A, B, C all are same , isn't it?

The output should be -1 if

ikon_me @ 9 Apr 2010 06:53 PM

The output should be -1 if A,B,C all are same, isn't it?

Please figure it out yourself

writerAPR10 @ 9 Apr 2010 10:21 PM

Please figure it out yourself as it is part of solving the problem.

My program gives the correct

face @ 10 Apr 2010 05:55 AM

My program gives the correct output , but the judge still shows WA. Is the judge output correct ?

The judge output is correct.

triplem @ 10 Apr 2010 06:54 AM

The judge output is correct. Yours is not.

Question All numbers (even if

Multifarious @ 10 Apr 2010 08:50 AM

Question

All numbers (even if they start with zero) will have "N" digits. So if N = 4, "21" will be represented as "0021"? Also, this is true for the output? If "21" is the best answer for C, I output "0021"?

You have to output a

triplem @ 10 Apr 2010 09:02 AM

You have to output a permutation of the digits given you to in C. If C is 2010, then 0021 is a permutation of those digits. It doesn't make sense to say 'if 21 is the best answer', since 21 is not a permutation of those digits.

made it .... just about a day

f03nix @ 14 Apr 2010 12:41 AM

made it .... just about a day late :P

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com