"not more than 1000000 digits" means an efficient in-place solution is needed. My first solution was string<->int conversion based. It worked for small ints, but got TLE with large numbers.

Thanks to http://www.bytehood.com/spoj-the-next-palindrome/418/ that shed lights on me. The key idea is the same as my first idea: simply keep increasing digits near the center digit(s), and we only need figure out left half of the digits since it is mirrored palindrome.

(I saw a lot of rejudge requests in SPOJ comments.. several erroneous results got returned from AC code. Mine's also rejected due to wrong answer - I think rejudge is needed)

Corner cases are important to this problem: single digits, all 9s, carry-over situation etc. Here is my code:

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std; int dCarry = ;
int findLgRInx(char *str, int len, int i_l)
{
int i_mis = -;
int i = i_l;
while(i-- >= )
{
if(str[i] > str[len - - i])
{
i_mis = i;
break;
}
}
return i_mis;
} void incLHalf(char *str, int len, int i_l)
{
int i = i_l;
while(i >= )
{
int d = str[i] - '';
if(d < )
{
str[i] = (d + ) + '';
return;
}
else
{
str[i] = '';
if(i > )
{
int nd = str[i-]-'';
if(nd < )
{
str[i-] = (nd + ) + '';
return;
}
}
else
{
dCarry = ;
return;
}
}
i--;
}
} void copyl2r(char *str, int len, int i_l)
{
int i = i_l;
while(i >= )
{
str[len-i-] = str[i];
i--;
}
} void calc_next_palin(char *str)
{
unsigned len = strlen(str);
if(len == )
{
int i = atoi(str);
cout << (i + ) + i / << endl;
return;
} int i_l = , i_r = ;
if(len % == )
{
i_l = len / - ;
i_r = len / ;
int i_mis = findLgRInx(str, len, i_l);
if(i_mis != -)
{
copyl2r(str, len, i_mis);
cout << str << endl;
return;
}
else
{
incLHalf(str, len, i_l);
copyl2r(str, len, i_l);
if(dCarry == )
{
cout << str << endl;
}
else
{
cout << "" << str << "" << endl;
}
return;
}
}
else //odd
{
int i_c = len / ;
int i_mis = findLgRInx(str, len, i_c + );
if(i_mis != -)
{
copyl2r(str, len, i_mis);
cout << str << endl;
}
else
{
int dmid = str[i_c] - '';
if(dmid < )
{
str[i_c] = (dmid + ) + '';
}
else
{
str[i_c] = '';
incLHalf(str, len, i_c - );
copyl2r(str, len, i_c);
}
if(dCarry == )
{
cout << str << endl;
}
else
{
cout << "" << str << "" << endl;
}
}
return;
}
} int main()
{
int cnt; cin >> cnt;
if(cnt == ) return ; //
while(cnt --)
{
string str; cin >> str;
calc_next_palin((char*)str.c_str());
}
return ;
}

SPOJ #5 The Next Palindrome的更多相关文章

  1. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  2. spoj The Next Palindrome

    题意:比给出的数大的最小回文数. 先用前n/2长对称到后面,如果没变大,在中间加1,进位,再对称. //#pragma comment(linker,"/STACK:1024000000,1 ...

  3. bzoj 3768: spoj 4660 Binary palindrome二进制回文串

    Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...

  4. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  5. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  6. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

随机推荐

  1. Day07_面向对象第二天

    1.构造方法(掌握) 1.构造方法的特点(掌握)      A.方法名必须和类名保持一致      B.没有返回值类型并且没有具体的返回值   2.构造方法的作用(掌握)       给对象的属性初始 ...

  2. 多个Tomcat同时运行环境配置 - imsoft.cnblogs

    解压下载好的Tomcat压缩包,两次.此例中分别命名为tomcat和tomcat2. 1. 在MyEclipse中配置好第一个Tomcat环境,可以正常运行项目后. 2. 再配置tomcat2这个项目 ...

  3. 最大流 Dinic

    struct Dinic { struct node { int x,y,c,next; }line[MAXM]; int Lnum,_next[MAXN],dis[MAXN],dp[MAXN]; b ...

  4. App_Code 引起的 ambiguously 问题

    今天遇到了一个这样的问题,一个.net framework 4.0 的web application ,下面有一个App_Code文件夹,里面的一些公共类的build action 是 Compile ...

  5. CollectionsAPI

    鉴于总用,总结一下: 类别 方法 查找 binarySearch. 返回 enumeration.checkedSortedMap 判断 max.min 移动 reverse(List).revers ...

  6. CSS控制文本超出指定宽度后用省略号代替,CSS控制文本不换行

    CSS控制文本超出指定宽度后用省略号代替,CSS控制文本不换行. 一般的文字截断(适用于内联与块): .text-overflow {     display:block;/*内联对象需加*/     ...

  7. Hibernate设置派生属性(formula)

    一.Customer中包含的字段: private static final long serialVersionUID = 1L;    private Integer id;    private ...

  8. zf2 中 new Express 的用法

    在使用联表查询时,如查联表条件有多个也可以,但是连接条件在zf2中会自动给列加``符号,所以要加像id = 1 这样的,他自动给1加``就无示执行了,这时候可以使用new Express(''),他里 ...

  9. Understanding Delegates in C#

    要学东西,还是得看第一手资料,看二.三手资料难免误会... http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C

  10. java的nio之:java的nio系列教程之FileChannel

    一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...