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.
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.
For each test case, print the longest magic string that is lexicographically smallest in a single line.
Input 3 difference similarq caaat Output difference imilaq aat
|Time Limit:||0.200542 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, kotlin, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.