Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindromic numbers from 1 to 1000. He became very frustrated because there is nothing creative in the task. Observing his expression, the teacher replied, "All right then, you want hard stuff, you got it." Then he asks Vinci to write a palindromic number which is greater than the given number. A number is called palindromic when its digits are same from both sides. For example: 1223221, 121, 232 are palindromic numbers but 122, 211, 332 are not. As there can be multiple solutions, Vinci has to find the number which is as small as possible.

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Each case starts with a line containing a positive integer. This integer can be huge and can contain up to 105 digits.

Output

For each case, print the case number and the minimum possible palindromic number which is greater than the given number.

Sample Input

5

121

1

1332331

11

1121

Sample Output

Case 1: 131

Case 2: 2

Case 3: 1333331

Case 4: 22

Case 5: 1221

题意:输出比X大的第一个回文串。

思路:做过很多次了感觉,还是WA了几发。 按照如下几个步骤。

1,我们先按照左边几位对称到右边,如果比原串大,输出。

2,从中间到左边找第一个非‘9’的字符,+1,中间的全部变为‘0’。

3,全部都是‘9’,则长度+1,首尾为‘1’,其他为‘0’;

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char d[maxn],a[maxn];int cnt;
void put(int len)
{
if(len==-){
putchar(''); rep(i,,cnt-) putchar('');
putchar(''); putchar('\n'); return ;
}
rep(i,,len) putchar(a[i]); putchar('\n');
}
void cal()
{
cnt=strlen(d+);
rep(i,,cnt/) swap(d[i],d[cnt+-i]);
rep(i,cnt/+,cnt) a[i]=d[i];
rep(i,,cnt/) a[i]=a[cnt+-i];
for(int i=cnt;i>=;i--)
if(a[i]>d[i]){
put(cnt); return ;
}
else if(a[i]<d[i]) break;
rep(i,cnt/+,cnt){
if(a[i]<'') {
a[i]++;
rep(j,cnt-i+,i-) a[j]='';
a[cnt+-i]=a[i];
put(cnt); return ;
}
}
put(-); return ;
}
int main()
{
int T,C=,x;
scanf("%d",&T);
while(T--){
scanf("%s",d+);
printf("Case %d: ",++C);
cal();
}
return ;
}

LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)的更多相关文章

  1. LightOJ 1205 Palindromic Numbers

    数位DP.... Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %l ...

  2. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  3. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  4. Lightoj1205——Palindromic Numbers(数位dp+回文数)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  5. BFS:Open and Lock(一个数的逐位变化问题的搜索)

    解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...

  6. [暑假集训--数位dp]LightOj1205 Palindromic Numbers

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  7. led跑马灯多种方法(移位法,位拼接法,调用模块法,位移及位拼接语法,testbench的理解,源文件的存储路径,计数器的个数,调用模块的方式)

    跟着教程写了几种方法,才发现自己写的虽然能实现,但比较繁琐.教程有三种方法: 1.移位法,每次左移一位,相比我自己写的,优点是不用把每一种情况都写出来.但是需要考虑左移到最后一位时需要自己再写个赋值语 ...

  8. 【LightOJ - 1205】Palindromic Numbers

    [链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...

  9. Palindromic Numbers LightOJ - 1205

    题目大意: 求区间内的回文数个数 题目思路: 数位dp,先枚举前一半数字,然后填上相应的后一半数字. #include<cstdio> #include<cstring> #i ...

随机推荐

  1. 常微分方程初值问题:多步预测-修正方法 [MATLAB]

    #先上代码后补笔记# #可以直接复制粘贴调用的MATLAB函数代码!# 1. 亚当斯(Adams)预测-修正算法 由亚当斯-巴什福特(Adams-Bashforth)显式预测公式和亚当斯-莫顿(Ada ...

  2. activiti整合spring

    activiti的配置文件其实就是一份spring的配置文件,只是默认将processEngineConfiguration做为一个bean来读取. 当和spring进一步整合时,需要使用 Sprin ...

  3. cocos进阶教程(3)Lua加密技术

    如果开发者不想让游戏中的资源或脚本文件轻易的暴露给其他人,一般会采用对文件进行加密的方式来保护文件或资源被盗用.Quick-Cocos2d-x 为开发者提供了xxtea加密算法,用来对脚本文件及资源进 ...

  4. Excel error 64-bit version of SSIS

    问题是 在windows server 2008 64位的计划任务执行 ssis 的错误 ,ssis你们带有读取excel 日期 2015/3/17 11:50:34日志 作业历史记录 (SSIS_U ...

  5. 2018 Multi-University Training Contest 4 Solution

    A - Problem A. Integers Exhibition 留坑. B - Problem B. Harvest of Apples 题意:计算$\sum_{i = 0}^{i = m}C( ...

  6. Python 日历模块calendar.monthrange 获取某一个月有多少天

    import calendar monthRange = calendar.monthrange(2018, 10) (0, 31) 输出的是一个元组: 第一个元素,数字0是这个月的第一天是星期天(上 ...

  7. python遗留问题

    def assert_element_in_page_source(s): print type(s) print s #assert s in driver.page_sourcecommand=' ...

  8. [one day one question] express 不缓存如何实现

    问题描述: express 默认缓存,这怎么破? 解决方案: apiRoutes.use(function (req, res, next) { res.setHeader('Cache-Contro ...

  9. Python面试题目之深浅拷贝浅析

    # copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. **只是简单的指针赋值 # copy.deepcopy 深拷贝 拷贝对象及其子对象 **指针赋值,且内容拷贝 用一个简单的例 ...

  10. Windows10下用Anaconda3安装TensorFlow教程【转】

    本文转载自:https://www.cnblogs.com/HongjianChen/p/8385547.html 1. 安装好Anaconda3版本 (1) 注:可以发现最新版本是Anaconda5 ...