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," ...
随机推荐
- java 的复用工具 - jar包
前言 Java提供了jar包的机制,使得已经开发好了的类能够顺利的被将来的工程所复用. 本章主要讲解如何使用这种工具. 包的作用 包能够将不同功用的类组织起来,从而确保类名的唯一性. 为了保证包名的唯 ...
- Delphi第一个入门程序——鼠标点击计数 - imsoft.cnblogs
实现的效果如下: 制作要点: 添加一个按钮Button1和一个标签Label1,并双击按钮进入编程界面在var Form1: TForm1;下面一行加上 n:integer;//定义变量. 然后在 ...
- Sprint第二个冲刺(第二天)
一.Sprint 计划会议: 在这次会议中我们主要讨论了我们正在做的几个任务,比如说在美化按钮和增添图片上我们都发表了自己的想法,卓炜杰同学也把我们的想法进行分析,寻求最适合的方法.在查看用 ...
- Junit单元测试细节
1.中心思想: 单元测试不是证明你对,而是证明你没错 2.基本注解应用 注解 使用环境 @Test 标志这个方法需要单元测试 @BeforeClass 在所有单元测试方法前执行 ps:需要是stati ...
- 2015GitWebRTC编译实录5
2015.07.20 libaudio_encoder_interface/libaudio_decoder_interface 编译通过将encoder,decoder两个lib合并了,后面需要看看 ...
- Oracle字符集的查看查询和Oracle字符集的设置修改
本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...
- JavaWeb学习记录(十九)——jstl自定义标签之简单标签
一.简单标签共定义了5个方法: setJspContext方法 setParent和getParent方法 setJspBody方法 doTag方法 二.方法介绍 osetJspContext方法 用 ...
- 记录Cat类的个体数目
B.记录Cat类的个体数目 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 22 (17 users) Total Accepted: ...
- 升级win10的理由
微软也没给我钱,我免费给它打了次广告. 我还是非常喜欢linux的,无奈公司深度依赖windows. 废话不多说,直接进入主题: [开机速度] 这里先说句题外话,不那么缺钱的兄弟,一定要去换一块SSD ...
- NetStatusEvent info对象的状态或错误情况的属性
代码属性 级别属性 意义 "NetStream.Buffer.Empty" "status" 数据的接收速度不足以填充缓冲区.数据流将在缓冲区重新填充前中 ...