"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. Bandicam视频录制技巧总结+小丸工具箱压缩视频解决视频体积问题

    1.视频录制. 录制质量建议选择100,保证原文件的质量才能更好地保证渲染转码后输出视频的质量.音效这里就一个关键点,就是编码器默认的MPEG-1 L2,会导致会声会影渲染输出出错,程序强行关闭,Ve ...

  2. sprintf 用法

    字符串格式化命令,主要功能是把格式化的数据写入某个字符串中 试试下面的代码就知道了 #include<cstdio> #include<cstdlib> using names ...

  3. 图像处理之image stitching

    背景介绍 图像拼接是一项应用广泛的图像处理技术.根据特征点的相互匹配,可以将多张小视角的图像拼接成为一张大视角的图像,在广角照片合成.卫星照片处理.医学图像处理等领域都有应用.早期的图像拼接主要是运用 ...

  4. form的onsubmit事件--表单提交前的验证最佳实现方式

    今天遇到了一个问题,页面中include了很多的公共页面(都是没有form的),并且里面好多的地方都是自行提交的(页面中加入一个type=“submit”域,然后js中写入sumbit.click来执 ...

  5. c 函数及指针学习 3

    strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0 1 2 ...

  6. 转:struts标签之select详解

    <html:select>生成HTML<select>元素 <html:option>:生成HTML<option>元素 <html:option ...

  7. Oozie的安装过程

    依赖CDH5,JDK和关系数据库 集群规划 主机名   IP                      Ooize节点 CHD1    XX.XX.XX.XX  oozie server,oozie ...

  8. 深入理解html5系列-文本标签

    转:http://blog.csdn.net/lihui130135/article/details/45150501 文章简介:       关于html5相信大家早已经耳熟能详,但是他真正的意义在 ...

  9. 自我提升mysql

    1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify  "co ...

  10. 关于usr/bin/ld: cannot find -lxxx问题总结

    /usr/bin/ld: cannot find -lxxx问题总结   linux下编译应用程序常常会出现如下错误:     /usr/bin/ld: cannot find -lxxx       ...