ZOJ - 3816 Generalized Palindromic Number dfs
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的更多相关文章
- [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limit: 65536 KB A number that will ...
- PAT 甲级 1019 General Palindromic Number(简单题)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- General Palindromic Number (进制)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- Palindromic Number (还是大数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT1019:General Palindromic Number
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
- 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 ...
- 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 ...
- A1019. General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
随机推荐
- abp viewmodel的写法
我的写法 public class QuotaCreateOrEditViewModel { public QuotaDto LoanQuota { get; set; } public bool I ...
- java 正则表达式如何提取中文的问题
String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...
- WPF使用附加属性绑定,解决data grid列绑定不上的问题
背景 需要对datagrid的列header添加自定义属性,然后绑定,并根据不同的列header绑定不同的值,传统的加扩展类太麻烦,而附加属性的特点更适用于这种场景. 1.xaml 代码 <Da ...
- 函数的参数是函数,函数中Ajax返回的回调函数中的函数运行
调用函数 checkAjax('addrinfo',formdata,vzxcv); 函数checkAjax function checkAjax(url,formdata,call_back){ / ...
- Bootstrap历练实例:警告样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- ios retain copy assign相关
assign: 简单赋值,不更改索引计数copy: 建立一个索引计数为1的对象,然后释放旧对象retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1 Copy其实是建立了一 ...
- 【分块】[HNOI2010]弹飞绵羊&分块大法祭
分块(似乎还有一种动态树(LCT)做法) 第一次学习分块,似乎有点小激动 这是黄学长的分块入门博客「分块」数列分块入门1 – 9 by hzwer 题目描述 某天,Lostmonkey发明了一种超级弹 ...
- (35)zabbix Event acknowledgment事件确认
概述 以往服务器出现报警,运维人员处理完事之后,报警自动取消,但是下一次出现同样一个错误,但是换了一个运维人员,他可能需要重新排查问题,直到问题处理完毕. 针对这种情况,zabbix提供了event ...
- Web框架之Django_04 模板层了解(过滤器、标签、自定义过滤器、标签、inclusion_tag、模板的继承与导入)
摘要: 模版层(模板语法) 模板语法 过滤器 标签 自定义过滤器.标签 inclusion_tag 模板的继承 模板的导入 一.模板语法: 常用语法:{{ }} 变量相关{% %} ...
- leetcode-2-basic
解题思路: 题目本身挺简单的,考虑用set,判断每个单词的字母是不是属于同一个集合.需要注意的是:1)set的构造方法:2)单词可能是大小写混合的,不一定只是首字母大写: 3)break是跳出循环=. ...