题目大意就是给出一个数,就比他大的最近的一个回文数,而数据范围到了10^1000000,所以暴搜之类的肯定不行的。

所以就要用字符串处理了。

首先如果输入数包含的数字全是9的话,那么显然应该输出100......0001这样的数,也只有这个情况会出现结果比输入的长度增1。

然后输入的是个位数的话,就输出下一个个位数就行,当然了,9除外。

然后就要把整个数字分为左右两半部分,从中间开始,用左边的数字分别与右边相对位置的数字比较,只要一出现大于的,后面的就不用判断了,那么就表明了用左边这个数字复制到右边,必然比原来那个数大,那么,只要一出现小于的,同样的道理。然后还有一种是本身是回文数的,就是左右一直相等呗。

然后如果左边大于右边的,直接把左半部复制到右半部,输出就行了。

其他的,小于的和相等的,就要把最中间的先加1,然后如果有进位,一位一位的往左边进就行了,最后再把左边的复制到右边就行了。

ID DATE USER PROBLEM RESULT TIME MEM LANG
5851068 2011-10-17 06:58:07 wwj The Next Palindrome accepted
edit  run
0.14 4.5M

C++

4.3.2

/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define LOCA
#define MAXN 500005
#define INF 100000000
#define eps 1e-7
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;
char s[1000005];
char ans[1000005];
int len, ti, tj;
bool check(char *str)
{int i;for(i = 0; i < len; i++){if(str[i] != '9')return false;}return true;
}
bool ls(char *str)
{int p1, p2;if(len % 2 == 0){p1 = len / 2 - 1;p2 = len / 2;}else{p1 = p2 = len / 2;}ti = p1, tj = p2;if(len % 2 == 1){p1--;p2++;}while(p1 >= 0){if(str[p1] < str[p2])return false;else if(str[p1] > str[p2])return true;p1--;p2++;}return false;
}
int main()
{
#ifdef LOCALfreopen("d:/data.in","r",stdin);freopen("d:/data.out","w",stdout);
#endifint t, i;scanf("%d", &t);while(t--){scanf("%s", s);len = strlen(s);int cnt = 0;if(len == 1 && s[0] < '9') {s[0]++; printf("%s\n", s); continue;}if(check(s)){printf("1");for(i = 0; i < len - 1; i++)printf("0");printf("1\n");continue;}if(len % 2 == 0){for(i = 0; i < len / 2; i++)ans[cnt++] = s[i];for(i = len / 2 - 1 ; i >= 0; i--)ans[cnt++] = s[i];ans[cnt++] = '\0';}else{for(i = 0; i <= len / 2; i++)ans[cnt++] = s[i];for(i = len / 2 - 1; i >= 0; i--)ans[cnt++] = s[i];ans[cnt++] = '\0';}if(ls(s)){printf("%s\n", ans);continue;}ans[ti]++;ans[tj] = ans[ti];if(ans[ti] > '9')while(ans[ti] > '9' && ti >= 0){ans[ti] = '0';ans[tj] = ans[ti];ti--;tj++;ans[ti]++;ans[tj] = ans[ti];}printf("%s\n", ans);}return 0;
}

SPOJ 5 The Next Palindrome相关推荐

  1. SPOJ 5. The Next Palindrome

    大于某个数K的最小回文数,做的很纠结~对很多python细节还不是很熟.测试了找到的所有用例,结果是对的,但提交是TLE,暂时不管了,先放到这了. 主要分K中数字为1个和多个两种情况考虑 1. 如果K ...

  2. SPOJ - IITKWPCE Let us play with strings(回文自动机+Palindrome Series优化dp)

    题目链接:点击查看 题目大意:给出一个长度为 n 的字符串,问最少拆分成多少个连续的子串,使得每个子串都是一个回文串 题目分析:dp[ i ] 代表 s[ 1 : i ] 的前缀最少可以拆分成多少个连 ...

  3. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  4. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

  5. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  6. 234. Palindrome Linked List - Easy

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  7. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  8. Lintcode108 Palindrome Partitioning || solution 题解

    [题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...

  9. 数位DP 回文序列 POJ-3280 Cheapest Palindrome

    Cheapest Palindrome [ POJ - 3280 ] 题目大意: 给定字符串s,长度为m,由n个小写字母组成.在s的任意位置增删字母,把它变成回文串,增删特定字母的花费不同,求最小花费 ...

最新文章

  1. 微软的100道算法面试题(一)
  2. 训练softmax分类器实例_CS224N NLP with Deep Learning(四):Window分类器与神经网络
  3. 【招聘(北京)】.NETCORE开发工程师(微服务方向)
  4. 静茹docker容器的几种方法_1-容器和docker基础知识
  5. 4.聚合aggregate
  6. 以毒攻毒Fight Fire with Fire: Towards Robust Recommender Systems via Adversarial Poisoning Training论文解读
  7. 北京化工大学通信工程linux,北京化工大学通信工程专业解读
  8. 开关电源之EMI设计
  9. HDOJ 2018 母牛的故事
  10. Linux查看网卡带宽
  11. 1299元起!小米CC9系列发布:全系标配双旗舰相机和屏幕指纹
  12. LAMP 3.1 mysql的root密码重置
  13. 【poj1995】快速幂
  14. C语言编程之学用rectangle画方形
  15. Axure 8.1.0.3381版安装包+注册码百度云盘下载
  16. win10如何打来计算机的工具,电脑系统教程:Win10自带解压缩文件工具如何使用
  17. exls表格搜索快捷键_excel表格查找快捷键|excel表格的常用功能快捷键介绍
  18. signature=99d87437cab1487c89a59a65cc379430,剖析根据汉字转拼音的JQuery插件源码
  19. openbsd停止mysql_英特尔处理器超线程功能被OpenBSD 停用,并爆bug
  20. SQL - 数据查询语句之字符串拆分

热门文章

  1. Hash算法冲突解决方法分析
  2. 别随便安装 Pokemon GO被曝藏恶意后门
  3. 关于IPV6 的个人猜想
  4. 分享一个64位的PS安装包
  5. 自己游戏之旅————感言
  6. 【base64】java 通过图片的Base64字符串判断文件格式
  7. 愿世间再无人不懂正则
  8. MetisMenu : Jquery + CSS 实现可隐藏的二级侧边栏导航
  9. Eclipse下的SVN提交代码报错问题
  10. 男生给女生的N条提醒!