Sphere Online Judge

SPOJ Problem Set (classical)

5. The Next Palindrome

Problem code: PALIN

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

Input:
2
808
2133

Output:
818
2222

Warning: large Input/Output data, be careful with certain languages
Added by:Adrian Kosowski
Date:2004-05-01
Time limit:9s
Source limit:50000B
Languages:All except: CLOJ F# PERL 6 TECS

hide comments
2010-09-01 21:05:12 priyanka chatterjee
Consider a test case like 00000000000000 or 0000000000000007 . For the 1st case answer is 1 and second case answer is 8 , although it is stated in the problem that K is +ve, i got AC only after i resolved these cases
2010-08-29 07:05:14 deepakraj
nice:)
2010-08-28 09:43:10 Wojciech Pietrzak
http://gist.github.com/554317 - ruby-prof shows me times about 6.3 - 7.5 seconds, why I get TLE ?
2010-08-24 22:28:17 Mina Fouad
How to handle a 1000000 digits number in C++. Anyone who can submit an accepted solution tell me.
2010-08-22 13:06:01 M.Saranya
TLE - The run time of the program to give the output . Is it clear

Last edit: 2010-08-22 13:07:03
2010-08-12 18:04:56 sashidhar
what does an nzec error mean?
2010-08-07 23:16:47 Rudolf Brisuda
oh, sure 1000000 digits...
2010-08-06 16:12:11 Farshad soleimani asl
Very good QUES!
2010-07-27 09:16:49 Wojciech Pietrzak
http://gist.github.com/491877 - SIGABRT, why ?

Last edit: 2010-07-27 09:22:45
2010-07-26 09:53:32 Ricky Suryadharma
Not wondering again... :D
Just AC in Java.
The main problem, just I though, how to read/write I/O (not the algorithm).
Try to read byte by byte and write byte by byte (don't use Scanner class and System.out.println, they will slow program).

Hey, after I saw the status, my Java program runs faster than my C++ program (even I use the same algorithm).. LOL.. my first time experience XD
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.