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:2s-9s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6

hide comments
2014-09-23 22:42:39 Pawan Bathe
Can anybody help understand, why is this wrong <script src="http://ideone.com/e.js/jjIvBQ" type="text/javascript" ></script>?
2014-09-23 11:41:49 Huibert-Jan Nieuwkamer
Although it has been said before: For all of you lot using python and sys.stdin.readline(), use input() (or raw_input() depending on python #) instead to read input. The last line has no line ending character.
2014-09-21 20:54:45 RUBAL GUPTA
plzz help
2014-09-21 20:54:30 RUBAL GUPTA
my code is working in codeblock bt showing wrong anwer at sumbit
2014-09-18 04:42:57 lyn
AC finally as a SPOJ newbie.. the source code can be fairly simple if the correct strategy is chosen .. and as the guys have already pointed out, the 'integer K of not more than 1000000 digits' indicates using atoi only yields WA
2014-09-15 09:53:06 nero
why its showing wrong answer? its running perfectly in borland c++, dev c++ and gcc in linux.
ID-12377818

Last edit: 2014-09-15 09:55:33
2014-09-14 22:47:42 Ignacio Cortorreal
As Yevgen Yampolskiy said, last line does not have terminating '\n'. I'll consider that on next problems. :P
2014-09-14 17:23:05 Ignacio Cortorreal
Why is this answer wrong?

http://www.spoj.com/files/src/12373877/

Last edit: 2014-09-14 17:24:17
2014-09-14 00:49:53 akash agrawal
code is running successfully in ideone but
here it is given wrong answer
2014-09-08 16:11:17 Vishal
Running in ideone with time 0.11 here it shows time limit exceeded plz help

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String str=sc.next();
int no=str.length();
char ar[]=new char[no];
ar=str.toCharArray();
int flag=0,flag1=0;
for(int j=0;j<no/2;j++)
{
if(ar[j]!=ar[no-1-j])
{
flag++;
if(ar[no-j-1]<ar[j])
flag1++;
ar[no-j-1]=ar[j];
}

}
if(flag==0 || flag1==0)
{
if(no%2==0)
ar[no/2]=++ar[no/2-1];
else
ar[no/2]++;
}
for(int k=0;k<no;k++)
System.out.print(ar[k]);
System.out.println();
}
}
}
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.