SPOJ #5 The Next Palindrome
"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的更多相关文章
- SPOJ:The Next Palindrome(贪心&思维)
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- spoj The Next Palindrome
题意:比给出的数大的最小回文数. 先用前n/2长对称到后面,如果没变大,在中间加1,进位,再对称. //#pragma comment(linker,"/STACK:1024000000,1 ...
- bzoj 3768: spoj 4660 Binary palindrome二进制回文串
Description 给定k个长度不超过L的01串,求有多少长度为n的01串S满足: 1.该串是回文串 2.该串不存在两个不重叠的子串,在给定的k个串中. 即不存在a<=b<c<= ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- 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 ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
随机推荐
- MapBox TileMill
TileMill 学习网站: Walkthrough: Creating tiles with Mapnik using TileMill TileMill 快速入门 Cartography With ...
- libvirtError: no connection driver available for qemu:///system 解决办法
今天部署了一套K版的OpenStack环境后,启动nova-compute后,日志里狂报如下错误(因为其内部有定时任务需要去连libvirtd进程): 2015-08-14 10:01:00.061 ...
- Sublime Text 2 安装主题的方法
主题下载 下载一个主题,例如: https://github.com/hyspace/st2-reeder-theme 里面起作用的文件有两个: Reeder.sublime-theme Earths ...
- 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- P168 实战练习(权限修饰符)
创建Game类,运行代码如下: package org.hanqi.pn0120; public class Game { private String name; private String ca ...
- java多线程之:SynchronousQueue队列
SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有. 不能在同步队列上进行 peek ...
- HTTPS-透彻学习汇总
SSL和SSH和OpenSSH,OpenSSL有什么区别 一.SSL的作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. 窃听风险(eavesdroppin ...
- BEA-WEBLOGIC ---http://www.beansoft.biz/weblogic/docs92/index.html
WebLogic Home 英文对照 发行信息 站点地图 Installation Guide 新增功能 已知和已解决的问题 Upgrade Guide Installing Ma ...
- EasyUI之DataGrid使用
http://blog.csdn.net/liovey/article/details/9173931 背景介绍: 原 先项目采用普通的jsp页面来做为前端显示,用户体验差,并且为了实现某一种效果需要 ...