SPOJ #5 The Next Palindrome
2014-02-06 02:43
316 查看
"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:
View Code
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 = 0; int findLgRInx(char *str, int len, int i_l) { int i_mis = -1; int i = i_l; while(i-- >= 0) { if(str[i] > str[len - 1 - i]) { i_mis = i; break; } } return i_mis; } void incLHalf(char *str, int len, int i_l) { int i = i_l; while(i >= 0) { int d = str[i] - '0'; if(d < 9) { str[i] = (d + 1) + '0'; return; } else { str[i] = '0'; if(i > 0) { int nd = str[i-1]-'0'; if(nd < 9) { str[i-1] = (nd + 1) + '0'; return; } } else { dCarry = 1; return; } } i--; } } void copyl2r(char *str, int len, int i_l) { int i = i_l; while(i >= 0) { str[len-i-1] = str[i]; i--; } } void calc_next_palin(char *str) { unsigned len = strlen(str); if(len == 1) { int i = atoi(str); cout << (i + 1) + i / 9 << endl; return; } int i_l = 0, i_r = 0; if(len % 2 == 0) { i_l = len / 2 - 1; i_r = len / 2; int i_mis = findLgRInx(str, len, i_l); if(i_mis != -1) { copyl2r(str, len, i_mis); cout << str << endl; return; } else { incLHalf(str, len, i_l); copyl2r(str, len, i_l); if(dCarry == 0) { cout << str << endl; } else { cout << "1" << str << "1" << endl; } return; } } else //odd { int i_c = len / 2; int i_mis = findLgRInx(str, len, i_c + 1); if(i_mis != -1) { copyl2r(str, len, i_mis); cout << str << endl; } else { int dmid = str[i_c] - '0'; if(dmid < 9) { str[i_c] = (dmid + 1) + '0'; } else { str[i_c] = '0'; incLHalf(str, len, i_c - 1); copyl2r(str, len, i_c); } if(dCarry == 0) { cout << str << endl; } else { cout << "1" << str << "1" << endl; } } return; } } int main() { int cnt; cin >> cnt; if(cnt == 0) return 0; // while(cnt --) { string str; cin >> str; calc_next_palin((char*)str.c_str()); } return 0; }
View Code
相关文章推荐
- Fox and Cross
- 我在知乎回答关于 Linux C++ 服务端编程的学习方法
- Fox and Number Game
- Office 2013 专业增强版简体中文版官方下载
- IOS 7去除状态栏
- 寒假学习 第16.17天 (linux 高级编程)
- "ios模拟器未能安装此应用程序“解决办法
- 成功的软件开发者需要掌握人际技能的3个原因
- HDU 2059 龟兔赛跑(动态规划)
- LeetCode主题整理(4)链表及相关问题
- 02-解决不能在web.xml中配置action问题
- Java Socket通信(多线程)
- 理解Java中的前期绑定和后期绑定
- 敏捷开发之Scrum扫盲篇
- Ubuntu使用
- java WEB开发国际化(二)
- Java WEB开发--国际化(一)
- 理解Java中的协变返回类型
- c# 赋值后最后一项数据部分丢失
- 12306与抢票插件初识