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. 生成随机ID且唯一

    var T = [ {P:11,G:2}, {P:101,G:7}, {P:1009,G:26}, {P:10007,G:59}, {P:100003,G:242}, {P:1000003,G:568 ...

  2. 1658: Easier Done Than Said?

    1658: Easier Done Than Said? Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 15  Solved: 12[Submit][St ...

  3. a标签目标链接问题

    1.先确定开始文件和目标文件,例如从css.html开始到body.html 2.确定文件寻找路径,因为css.html的父目录是css,而body.html在body目录下,所以需要先退到上一目录h ...

  4. Linux C++/C开发所必需的一系列工具

    系统平台下的开发工具.开发环境各有不同.Linux C++/C开发所必需的一系列工具: 1. vi(vim)文本编辑器一个UNIX世界标准的文本编辑器,简约而强大,不论作为开发人员还是系统管理员,熟练 ...

  5. jquery操作滚动条滚动到指定元素位置 scrollTop

    $('.brand_t a').bind('click',function(){ if($(this).attr('title1')){ var toChar = $(this).attr('titl ...

  6. shell脚本,判断给出的字符串是否相等。

    第一种方法[root@localhost wyb]# .sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" ...

  7. c# DateTime常用用法

    参考:http://sqlyuju.com/c-datetime-hh-hh-qubie.html https://www.cnblogs.com/xiongxiaobai/p/5282827.htm ...

  8. Leetcode 7 反转整数Reverse Integer

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: ...

  9. 主DNS服务-反向解析

    上篇说了主DNS正向解析 当中是有个小问题的,什么问题呢? 试问当我们输入wwww或ww或更多w的时候它还能解析出来吗? 或者不输入w的时候还能解析吗? 上篇没有定义是解析不了的,怎么定义呢?很简单, ...

  10. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...