Generalized Palindromic Number


Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number.

We call a number generalized palindromic number, if after merging all the consecutive same digits, the resulting number is a palindromic number. For example, 122111 is a generalized palindromic number. Because after merging, 122111 turns into 121 which is a palindromic number.

Now you are given a positive integer N, please find the largest generalized palindromic number less than N.

Input

There are multiple test cases. The first line of input contains an integer T (about 5000) indicating the number of test cases. For each test case:

There is only one integer N (1 <= N <= 1018).

Output

For each test case, output the largest generalized palindromic number less than N.

Sample Input

4
12
123
1224
1122

Sample Output

11
121
1221
1121

Author: LIN, Xi                                         Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

转自:http://blog.csdn.net/u011345136/article/details/39122741

题意:求小于N的回文数,这个数的回文相同的数可以缩成一个数
思路:dfs(l, r, flag):l代表左边侧长度,r代表右边的长度,flag代表是否处于边界,然后在搜索右边匹配左边的同时,枚举k,k代表右边连续相同的数都匹配左边的个数

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 55
#define M 15
#define mod 6
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
ll n;
char s[N];
ll ans;
int len;
char leftnum[N];
char rightnum[N]; void ini()
{
// scanf("%lld",&n);
cin>>n;
ans=-;
//memset(left,0,sizeof(left));
//memset(right,0,sizeof(right));
sprintf(s+,"%lld",n);
//sprintf(s+1,"%I64d",n);
len=strlen(s+);
for(int i=;i<=len;i++){
s[i]-='';
}
} void out()
{
//printf("%lld\n",ans);
cout<<ans<<endl;
} ll getans(int l,int r)
{
ll re=;
int i;
for(i=;i<=l;i++){
re=re*+leftnum[i];
}
for(i=r;i>=;i--){
re=re*+rightnum[i];
}
return re;
} ll dfs(int l,int r,int flag)
{
ll re=-;
int i,k,m;
if(l+r->len) return -1ll;
if(l+r-==len){
re=getans(l-,r);
if(re>=n) return -1ll;
return re;
} m= (flag==) ? s[l] : ;
for(i=m;i>=;i--)
{
leftnum[l]=i;
if( (l== || leftnum[l]!=leftnum[l-]) && !(l== && i==) && (l+r!=len) )
{
for(k=;k+l+r<=len;k++){
rightnum[k+r]=i;
re=max(re,dfs(l+,r+k,flag && (m==i) ) );
}
}
else{
re=max(re,dfs(l+,r,flag && (m==i) ));
}
if(re>){
return re;
}
}
return re;
} int main()
{
//freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
ans=dfs(,,);
out();
} return ;
}

ZOJ - 3816 Generalized Palindromic Number dfs的更多相关文章

  1. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  2. PAT 甲级 1019 General Palindromic Number(简单题)

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  3. General Palindromic Number (进制)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  4. Palindromic Number (还是大数)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  5. PAT1019:General Palindromic Number

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  6. 1024 Palindromic Number int_string转换 大整数相加

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. PAT A1024 Palindromic Number (25 分)——回文,大整数

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  8. PAT A1019 General Palindromic Number (20 分)——回文,进制转换

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  9. A1019. General Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

随机推荐

  1. HDOJ 4509 湫湫系列故事——减肥记II(2013腾讯编程马拉松) 并查集合并区间

    发现这种合并区间的题目还可以这么玩 给你n段时间 然后问没被占用的时间是多少 题目所给的区间是右开的导致我wa 好多人5e5*1440的暴力跑出来的时间居然只是我的两倍 不懂.... 所以并查集并没有 ...

  2. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...

  3. H5新特性:

    新增选择器 document.querySelector.document.querySelectorAll 拖拽释放(Drag and drop) API 媒体播放的 video 和 audio 本 ...

  4. Robot Framework(十二) 执行测试用例——配置执行

    3.4配置执行 本节介绍可用于配置测试执行或后处理输出的不同命令行选项.与生成的输出文件相关的选项将在下一节中讨论. 3.4.1选择测试用例 通过测试套件和测试用例名称 按标签名称 当没有测试匹配选择 ...

  5. 各种分布(distribution)

    正态分布(Normal distribution),又名高斯分布(Gaussian distribution).若随机变量X服从一个数学期望为μ.方差为σ^2(标准差为σ)的正态分布,记为N(μ,σ^ ...

  6. 【转载】WPF DataGrid 性能加载大数据

    作者:过客非归 来源:CSDN 原文:https://blog.csdn.net/u010265681/article/details/76651725 WPF(Windows Presentatio ...

  7. 网络流的$\mathfrak{Dinic}$算法

    网络流想必大家都知道,在这不过多赘述.网络流中有一类问题是让你求最大流,关于这个问题,许多计算机学家给出了许多不同的算法,在这里--正如标题所说--我们只介绍其中的一种--\(\tt{Dinic}\) ...

  8. Apache安装错误 APR not found解决方法

    在配置Apache的时候,出现错误 原因是缺少一些依赖包,安装这些依赖包就行了 下载依赖包,注意我这里下载的与参考链接上的有些不同,安装上也有不一样 wget http://archive.apach ...

  9. python--操作系统介绍,进程的创建(并发)

    一 .  操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬件的竞争变得有序 二 多道技术: 所谓多道程序设计技术,就是指允许多个程序同时进入内存 ...

  10. LeetCode(136) Single Number

    题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...