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. The adidas NMD Camo Singapore consists of four colorways

    Next within the popular selection of the adidas NMD Singapore is really a clean all-black form of th ...

  2. rails性能优化

    1,使用Unicorn或者Thin服务器替代默认的webrick.2,静态资源压缩合并,放到云存储上.3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js ...

  3. vue版本,小Toast

    <div id="message" :class="{'show':show_Message}"><p v-html="messag ...

  4. SVD在餐馆菜肴推荐系统中的应用

    SVD在餐馆菜肴推荐系统中的应用 摘要:餐馆可以分为很多类别,比如中式.美式.日式等等.但是这些类别不一定够用,有的人喜欢混合类别.对用户对菜肴的点评数据进行分析,可以提取出区分菜品的真正因素,利用这 ...

  5. CCPC-Wannafly Winter Camp Day5 (Div2, onsite)

    Replay: Dup4: 时间复杂度算不对? 一点点思路不经过验证就激动的要死? 浪费自己一个小时还浪费别人一个小时? 对1e3不敏感? 1e3 * 1e3是多少? 模拟建边跑dp不写非要写个大模拟 ...

  6. 常用php操作redis命令整理(三)LIST类型

    LIST 头元素和尾元素:头元素指的是列表左端/前端第一个元素,尾元素指的是列表右端/后端第一个元素.举个例子,列表list包含三个元素:x, y, z,其中x是头元素,而z则是尾元素.空列表:指不包 ...

  7. 封装JS实现Ajax

    这两天仔细理解了一下Ajax,然后整理封装了一下,如果有什么不对的地方,请指教,谢谢! AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScri ...

  8. xlrd基本操作

    #coding=utf-8import xlrd def read_excel(): workbook = xlrd.open_workbook('people.xlsx') sheet2 = wor ...

  9. 16s workfollw

    http://bioconductor.org/packages/devel/bioc/vignettes/metagenomeFeatures/inst/doc/Example_16S_Annota ...

  10. python第三方库PIL安装的各种坑

    PIL python的图像处理库,pycharm安装屡次失败,始终报错 果断换依旧报错,查遍所有博客, 有给pip加参数的,pip install PIL --allow-external PIL - ...