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

解题思路:

题意为给定一个数n。求小于n的最大“回文数”, “回文数”要求连续同样的数能够压缩成一个数,比方1233221,能够压缩成12321,是回文数。

思路为暴力枚举,相当于填数字。首先枚举填左边第一个数字从大到小。然后在里面枚举右边有多少个和它一样的数,注意剪枝,左边当前数字假设符合题意的话,就不用再继续枚举当前数字了。由于要求最大。详细思路在代码凝视中。

參考:http://blog.csdn.net/u011345136/article/details/39122741

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define ll long long
const int maxn=20;
int Left[maxn],Right[maxn];//回文数左部分和右部分,比方 1511 ,左边存1 5 右边存 1 1
string str;//输入字符串
int len;//长度
ll n;//字符串代表的数字 ll lmax(ll a ,ll b)
{
return a>b?a:b;
} ll getNum(int l,int r)//获得当前回文数的值
{
ll ans=0;
for(int i=1;i<=l;i++)
ans=ans*10+Left[i];
for(int i=r;i>=1;i--)//注意存储的顺序,原数的最后一位是Left数组存储的第一位
ans=ans*10+Right[i];
return ans;
} ll dfs(int l,int r,int cur)//左边数字的个数l,右边数字的个数r,当前数字是否是边界。比方2511,第一位2就是边界,回文数该位要<=2。其它位就不是边界,能够从9開始
{
ll ans=-1;
if(l+r-1==len)//由于dfs參数中的l。dfs中给左边第l位赋值。而递归參数为l+1,所以当(l-1+r)=len时才是递归出口
{
ans=getNum(l-1,r);
if(ans>=n) return -1;//别忘了,不能大于等于给定的数 T T
return ans;
}
int now=cur? (str[l-1]-'0'):9;//给左边第l位赋值。推断是否为边界
for(int i=now;i>=0;i--)//枚举第l位
{
Left[l]=i;
//假设 当前l是第一位或者不是第一位但当前位和前一位不一样 且 不能l是第一位且i为0 且 数字还没有填完
if((l==1||(l>1&&Left[l]!=Left[l-1]))&&(!(l==1&&i==0))&&l+r!=len)
{
for(int k=1;l+r+k<=len;k++)//枚举右半部分与第l位同样的有多少位
{
Right[r+k]=i;
ans=lmax(ans,dfs(l+1,r+k,cur&&now==i));
}
}
else
{
ans=lmax(ans,dfs(l+1,r,cur&&now==i));
}
if(ans>0)//假设当前的ans符合题意,就不用再向下搜索了。比方455 第1位4找到一个数符合题意454,那么第一位就不用枚举3 2 1 0了剪枝
return ans;
}
return ans;
} int main()
{
int t;cin>>t;
while(t--)
{
cin>>str;
n=0;
len=str.length();
for(int i=0;i<len;i++)
n=n*10+str[i]-'0';
ll ans=dfs(1,0,1);
cout<<ans<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)的更多相关文章

  1. ZOJ - 3816 Generalized Palindromic Number dfs

    Generalized Palindromic Number Time Limit: 2 Seconds                                     Memory Limi ...

  2. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  3. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  4. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

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

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

  6. 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想

    http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...

  7. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  8. Codeforces Beta Round #51 B. Smallest number dfs

    B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...

  9. General Palindromic Number (进制)

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

随机推荐

  1. hdu4738(割桥)

    找人去炸边,炸完之后分成两个连通块(炸割桥) 每条边上有w个守卫,派去炸桥的人不能比守卫少 所以, 如果原本不连通,那么输出0 如果没有桥,输出-1 如果有桥没有守卫,那么是输出1,而不是0(tric ...

  2. codeforece Round#311 BCDE

    B题 给我们n,m ,  m表示茶壶的容量 接下来2*n个数字,表示茶杯的容量,将这些茶杯分给n个男孩和n个女孩 可以倒x毫升的茶水给每个女孩,那么就要倒2x毫升的茶水给男孩,当然了,茶杯要装的下,且 ...

  3. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  4. Objective-C路成魔【18-复制对象】

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 将一个变量 ...

  5. MySQL 批量Dll操作(转)

    概述 本章节介绍使用游标来批量进行表操作,包括批量添加索引.批量添加字段等.如果对存储过程.变量定义.预处理还不是很熟悉先阅读我前面写过的关于这三个概念的文章,只有先了解了这三个概念才能更好的理解这篇 ...

  6. DBMS_STATS.GATHER_TABLE_STATS

    由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要! 作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直方图信 ...

  7. linux 手动安装 oracle(转)

    Linux下安装Oracle 11 此为参照CU论坛上的高人写的文章并结合自身环境增加了点细节性的东西 操作系统 CentOS 4.2 2.6.11.8内核 安装准备 下载Oracle11安装包 内存 ...

  8. gradle(转)

    一.声明dependency     在build.gradle文件编辑以下代码: apply plugin: 'java' repositories { mavenCentral() } depen ...

  9. JDBC batch批量Statement executeBatch 详细解释

    JDBC提供了数据库batch处理的能力,在数据大批量操作(新增.删除等)的情况下能够大幅度提升系统的性能.我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系 ...

  10. 由单页面web应用引发的企业应用问题

    由于单页面web应用的流行,client与server端之间都对应的产生了一些微妙的变化,比方,client原来仅仅是用来展示页面和理清逻辑,而现在逐渐转变成了一个可以进入驱动状态的应用程序. 未来的 ...