LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)
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)(逐位确定法)的更多相关文章
- LightOJ 1205 Palindromic Numbers
数位DP.... Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %l ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- BFS:Open and Lock(一个数的逐位变化问题的搜索)
解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...
- [暑假集训--数位dp]LightOj1205 Palindromic Numbers
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- led跑马灯多种方法(移位法,位拼接法,调用模块法,位移及位拼接语法,testbench的理解,源文件的存储路径,计数器的个数,调用模块的方式)
跟着教程写了几种方法,才发现自己写的虽然能实现,但比较繁琐.教程有三种方法: 1.移位法,每次左移一位,相比我自己写的,优点是不用把每一种情况都写出来.但是需要考虑左移到最后一位时需要自己再写个赋值语 ...
- 【LightOJ - 1205】Palindromic Numbers
[链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...
- Palindromic Numbers LightOJ - 1205
题目大意: 求区间内的回文数个数 题目思路: 数位dp,先枚举前一半数字,然后填上相应的后一半数字. #include<cstdio> #include<cstring> #i ...
随机推荐
- 给iphone配置qq邮箱
在手机上使用qq邮箱发送和接受邮件,但是又不用qq邮箱,我用的是“网易邮箱大师” ,那么就需要配置服务. 1.在qq邮箱中设置邮箱,开启相关的服务,然后用手机发送短信来生成授权码.最后在手机上设置的密 ...
- Java 和 Python 解析动态 key 的 JSON 数据
一.概述 解析JSON过程中,什么情况都可能遇到.遇到特殊的情况,不会怎么办?肯定不是设计的问题,一定是你的姿势不对. 有这样一种JSON需要解析: { "b3444533f6544&quo ...
- Ubuntu安装 jdk.rpm 报错问题解决
报错问题出现原因 第一次使用Ubuntu操作系统,很多命令及软件安装方式与以往使用的Linux操作系统(CentOS)有很大区别.现在总结使用Ubuntu在安装JDK中,遇到的问题及解决方法. roo ...
- linux下如何退出tmux和重新进入tmux
1.退出(detach)当前tmux ctrl+d 2.重新进入tmux tmux attach -t <target-session> 如:当前有很多session,那么选择哪一个呢? ...
- POJ 3422 Kaka's Matrix Travels(拆点+最大费用流)题解
题意:小A从左上角走到右下角,每个格子都有一个价值,经过这个格子就把价值拿走,每次只能往下或往右走,问你走k次最多能拿多少价值的东西. 思路:这里有一个限制条件就是经过之后要把东西拿走,也就是每一格的 ...
- CF_884_F(NetFlow)
codeforces_884_f 题目大意:给出一串长为n的字符串(保证n为偶数),定义反回文串为每一个位置的对应位置上的字母都不等于它(for each i : s[i] != s[n+1-i]), ...
- POJ Stockbroker Grapevine(floyd)
https://vjudge.net/problem/POJ-1125 题意: 题意不是很好理解,首先输入一个n,表示有n个股票经纪人,接下来输入n行,每行第一个数m为该股票经纪人认识的经纪人数,然后 ...
- 使用javascript模拟常见数据结构(二)
四.链表 每种语言都实现了数组.这种数据结构非常方便,提供了一个便利的[]语法来访问它的元素.然而,这种数据结构有一个缺点:(在大多数语言中)数组的大小是固定的,从数组的起点或中间插入或移除项的成本很 ...
- Angel 实现FFM 一、对于Angel 和分布式机器学习的简单了解
Angel是腾讯开源的一个分布式机器学习框架.是一个PS模式的分布式机器学习框架. https://github.com/Angel-ML/angel 这是github地址. 我了解的分布式机器学 ...
- TinyURL
2018-03-09 15:19:04 TinyURL,短地址,或者叫短链接,指的是一种互联网上的技术与服务.此服务可以提供一个非常短小的URL以代替原来的可能较长的URL,将长的URL地址缩短. 用 ...