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
    • February Long Contest
    • January CookOff
    • January Long Contest
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • 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
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » November 2009 (Contest X) » Magic Strings

Magic Strings

Problem code: J5

  • All Submissions

All submissions for this problem are available.

Magic strings, invented by the Bytelandians, are strings that contain immense magical power within themselves. Magic strings could bring luck and happiness to the Bytelandian citizens. Formally, a string S of length n is a magic string if it satisfies the following conditions:

  • All letters of S are lowercase letters of the English alphabet.
  • Si is lexicographically smaller than Sn-i+1 for all odd i from 1 to [n/2].
  • Si is lexicographically greater than Sn-i+1 for all even i from 1 to [n/2].

(Si (1 ≤ i ≤ n) denotes the ith character of S). For example, the word "difference" is a magic string, while "similar" is not.

Given a string S of lowercase English letters, write a program to find the longest magic string than can be obtained by removing some letters of S. If there are more than one solutions, choose the longest magic string which is lexicographically smallest.

Input

The first line contains t, the number of test cases (about 10). Then t test cases follow. Each test case contains a string S written in a single line. S does not contain more than 2000 letters.

Output

For each test case, print the longest magic string that is lexicographically smallest in a single line.

Example

Input
3
difference
similarq
caaat

Output
difference
imilaq
aat

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


  • Submit

Comments

  • Login or Register to post a comment.

Can any 1 eloborate w.r.t

sunthoshbabu @ 2 Nov 2009 12:03 PM

Can any 1 eloborate w.r.t "similarq"?

 

How we get "imilaq"? I thought  it to be  "imilarq" ?

The sample output is correct.

triplem @ 2 Nov 2009 01:58 PM

The sample output is correct.

Re-read the problem

admin @ 2 Nov 2009 02:01 PM

Re-read the problem statement. 'imilarq' is not a magic string.

admin, please delete the

triplem @ 3 Nov 2009 09:26 AM

admin, please delete the previous two comments ASAP :(

plz xplain the xample:-- for

gurpreet_09 @ 3 Nov 2009 02:22 PM
plz xplain the xample:-- for difference isn't it that ceeffiendr the corrsct answer because cd,ee,fa alsso of length 5 and lexicographically smaller than caaat also if we cionsider for 3 letters word isn't it that aac is correct answer because aat>aac lexicographically plz correct me if i m wrng thnx in advance

plz xplain the xample:-- for

gurpreet_09 @ 3 Nov 2009 02:29 PM
plz xplain the xample:-- for difference isn't it that ceeffiendr the corrsct answer because cd,ee,fa alsso of length 5 and lexicographically smaller than caaat also if we cionsider for 3 letters word isn't it that aac is correct answer because aat>aac lexicographically plz correct me if i m wrng thnx in advance

sry my mistake must have read

gurpreet_09 @ 3 Nov 2009 03:02 PM

sry my mistake

must have read the question carefullyy

i hv understood the question now......

I have a few doubts, would

geekoo @ 4 Nov 2009 10:09 PM

I have a few doubts, would appreciate it a lot of someone can clarify these.

1) According to the magic string condition, lesser than and greater than conditions are specified. What about equal to condition? Maybe my next question would answer this.

2) Are the inputs provided all already magic strings? As in, in the test cases on the server.

3) In the first test case in the example, there is no change in the output as compared to the input string. Why is this so? Shouldn't I mandatorily remove some characters to form a new magic string? I am unable to understand what happened here.

4) I am unable to understand the meaning of "longest string that is lexicographically smallest".

I am sorry if I throwing some clues in with these questions. Please ignore in such a case.

@Prashanth I guess equal is

gamabunta @ 5 Nov 2009 01:09 AM

@Prashanth I guess equal is not to be considered either smaller or greater because of sample test 3. Otherwise answer would have been "aaat" or at least "aaa"

My interpretation of "longest string that is lexicographically smallest" rests on a case like "similarq". obviously "imilar" and "imilaq" both are longest magic strings, but "imilaq" is smaller lexicographically - so, consider the set of solutions with longest magic strings and choose the lexicographically smallest among them

unfortunately, the solution is not so obvious to me (yet?)

are the judge's pc is pentium

shiplu @ 5 Nov 2009 10:43 PM

are the judge's pc is pentium 1 or even more backdated, my codes time complexity is about

8 * 10 ^7 in total(including 10 test cases). I am getting TLE, this is redicolous.

 

10^9 runs in 1 sec in a normal pc like celeron D.

The machine configurations

admin @ 6 Nov 2009 01:52 PM

The machine configurations can be found on the wiki at www.codechef.com/wiki .

I think you shouldn't blame

Walrus @ 7 Nov 2009 08:03 AM

I think you shouldn't blame the speed of the server. The fact that there are many people solved this problem on this server means that your algorithm may be just not fast enough.

Time Limit is Too strict. My

nokia @ 9 Nov 2009 09:14 AM

Time Limit is Too strict. My O(N^2) solution got TLE

Therefore your O(N^2)

triplem @ 9 Nov 2009 09:16 AM

Therefore your O(N^2) solution is too slow. 26 people have solved this problem, so the time limit is fine.

I got accepted Thanks for

nokia @ 9 Nov 2009 08:18 PM

I got accepted

Thanks for your reply

Again poor time limit got AC

nokia @ 9 Nov 2009 11:31 PM

Again poor time limit got AC vai using scanf

well got on quite late..

abhishek agarwala @ 11 Nov 2009 02:24 AM

well got on quite late.. tried 3 questions.. the magic string .. the sudokux.. and the box..

the box was pretty simple..

was able to develop algorithm but probably some error in writing the code so did not get the right answer..

guess you cant do 3 problems in a stretch of 12 hours after all..

no point continuing further.....

best of luck to all the guyz still trying...

:)

submit page for this prob is

anshulgoel @ 11 Nov 2009 03:53 AM

submit page for this prob is not loading :(

now it is..   admin, a good

anshulgoel @ 11 Nov 2009 04:14 AM

now it is..

 

admin, a good feature wud be showin the amount of time exceeded

There's no way of knowing

triplem @ 11 Nov 2009 05:29 AM

There's no way of knowing that; your program is stopped as soon as it exceeds the time limit, otherwise it could run for hours.

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.

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