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. Xcode编译工具

    一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...

  2. 2017年网络空间安全技术大赛部分writeup

    作为一个bin小子,这次一个bin都没做出来,我很羞愧. 0x00 拯救鲁班七号 具体操作不多说,直接进入反编译源码阶段 可以看到,只要2处的str等于a就可以了,而str是由1处的checkPass ...

  3. webuploader项目中多文件上传实例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. (19)zabbix Applications使用介绍

    介绍 Applications(我们翻译为应用程序)是item的一个组. 例如我们要监控MySQL,我们可以将所有和MySQL相关的item放到这个应用程序中. 例如MySQL的availabilit ...

  5. 小数据池 is 和 ==的区别

    小数据池 一.小数据池 1)代码块 python程序是由代码块构成的,一个代码块的文本作为pythont程序执行的单元 官方文档: A Python program is constructed fr ...

  6. Python9-封装-day26(大年初三)

    class Room: def __init__(self,name,length,width): self.__name = name self.__length = length self.__w ...

  7. (转)iOS完成学习路线

    转自 MJ大神博客 原文地址http://blog.csdn.net/q199109106q/article/details/8596506 晚特地花时间整理出了iOS的完整学习路线图,希望对大家有帮 ...

  8. C++中四种强制类型转换方式

    类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...

  9. Luogu 2627 修建草坪 (动态规划Dp + 单调队列优化)

    题意: 已知一个序列 { a [ i ] } ,求取出从中若干不大于 KK 的区间,求这些区间和的最大值. 细节: 没有细节???感觉没有??? 分析: 听说有两种方法!!! 好吧实际上是等价的只是看 ...

  10. SpringSecurity结合数据库表实现权限认证

    SpringSecurity结合数据表实现权限认证: 下面的案例是在SpringBoot框架实现的: 步骤一:准备数据库表 以下是五张表的脚本 ### 用户表 create table Sys_Use ...