LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j (inclusive).
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing two integers i j (0 ≤ i, j ≤ 1017).
Output
For each case, print the case number and the total number of palindromic numbers between i and j (inclusive).
Sample Input
4
1 10
100 1
1 1000
1 10000
Sample Output
Case 1: 9
Case 2: 18
Case 3: 108
Case 4: 198
题意:求区间的回文串数量。
思路:从两头向中间靠,前缀是否小于原数用tag表示,后缀是否小于原数用ok表示,注意后缀尽管后面的比原位大,但是前面的小一点可以抵消其效果。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll dp[][][][]; int d[],cnt;
ll get(int bg,int l,int r,int tag,bool ok)
{
if(r>l) return !tag||(tag&&ok);
if(!tag&&dp[bg][l][tag][ok]) return dp[bg][l][tag][ok];
int lim=tag?d[l]:; ll res=;
rep(i,,lim){
if(bg==l&&i==) continue;
bool g=ok;
if(ok) g=i<=d[r];
else g=i<d[r];
res+=get(bg,l-,r+,tag&&(i==lim),g);
}
return tag?res:dp[bg][l][tag][ok]=res;
}
ll cal(ll x)
{
if(x<) return 0LL;if(x==) return 1LL;
ll res=; cnt=;
while(x) d[++cnt]=x%,x/=;
rep(i,,cnt) res+=get(i,i,,i==cnt,true);
return res;
}
int main()
{
int T,C=; ll L,R; scanf("%d",&T);
while(T--){
scanf("%lld%lld",&L,&R); if(L>R) swap(L,R);
printf("Case %d: %lld\n",++C,cal(R)-cal(L-));
}
return ;
}
有部分数组没有必要:
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll dp[][]; int d[],cnt;
//tag维护前缀是否小于,ok维护后缀是否小于。维护二者不一样。
ll get(int bg,int l,int r,int tag,bool ok)
{
if(r>l) return !tag||(tag&&ok);
if(!tag&&dp[bg][l]) return dp[bg][l];
int lim=tag?d[l]:; ll res=;
rep(i,,lim){
if(bg==l&&i==) continue;
bool g=ok;
if(ok) g=i<=d[r];
else g=i<d[r];
res+=get(bg,l-,r+,tag&&(i==lim),g);
}
return tag?res:dp[bg][l]=res;
}
ll cal(ll x)
{
if(x<) return 0LL;if(x==) return 1LL;
ll res=; cnt=;
while(x) d[++cnt]=x%,x/=;
rep(i,,cnt) res+=get(i,i,,i==cnt,true);
return res;
}
int main()
{
int T,C=;ll L,R; scanf("%d",&T);
while(T--){
scanf("%lld%lld",&L,&R); if(L>R) swap(L,R);
printf("Case %d: %lld\n",++C,cal(R)-cal(L-));
}
return ;
}
LightOJ - 1205:Palindromic Numbers (数位DP&回文串)的更多相关文章
- light oj 1205 - Palindromic Numbers 数位DP
思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- poj 1159 dp回文串
题意:添加最少的字符使之成为回文串 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- HDU4745--区间DP+回文串
这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...
- CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...
- Leetcode0005--Longest Palindromic Substring 最长回文串
[转载请注明]http://www.cnblogs.com/igoslly/p/8726771.html 来看一下题目: Given a string s, find the longest pali ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
随机推荐
- CentOS7系统上部署.net core程序
一.准备工作 首先安装 xshell 和 xftp ,前者用于SSH连接Linux服务器,后者用于FTP上传下载文件. xshell和xftp个人使用是免费的,下载地址 之后分别输入用户名和密码登录主 ...
- python16_day39【算法】
复习: 1.递归 调用自身 结束条件 一.冒泡算法 def bubble_sort(numbs): for i in range(len(numbs)-1): # 这个循环负责设置冒泡排序进行的次数. ...
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution
A:Exam Solved. 温暖的签. #include<bits/stdc++.h> using namespace std; ; int k; char str1[maxn], st ...
- zw黑天鹅足彩实盘测试5月数据包
[文件说明] $mx1,是单日数据:$mx9,是日数据和 入选率:2%, 准确度:40% 盈利率:120%左右 目前在测试稳定性 5月1日-6月14日,实盘数据 $mx9,15061409x15061 ...
- supervisor安装及其配置
一.supervisor概述 supervisor是一个c/s系统,被用来在类Unix系统中监控进程状态.supervisor使用python开发. 服务端进程为supervisord,主要负责启动自 ...
- OpenVAS安装过程
OpenVAS安装过程 安装过程 检查安装状况 命令行下输入opensav-check-setup,显示错误NO CA certificate file,并显示解决方法 创建证书 输入命令openva ...
- 从IC设计业看中国企业之发展
从IC设计业看中国企业之发展 在半导体领域,国际平均毛利润水平为40%.去年IC设计年会中,中国半导体行业协会IC设计分会理事长魏少军指出,中国IC设计业平均毛利润水平比国际平均水平低了12.39 ...
- 简单方法实现无刷新提交Form表单
前几天遇到一个前端的问题.我希望提交表单后页面不跳转且不刷新当前页面,然而查了很多方法都没有解决. 由于Form 是提交后一定刷新页面的,所以我们可以用一个折中的办法.我们给Form 指定一个ifra ...
- Xcode7.2与iOS9之坑 (持续更新)
GitHub地址 前几天升级OS X EI Capitan 10.11.1, 以及Xcode7.1,正好赶上公司新产品上线,要做iOS9的适配,遇到各种坑,各种查资料,随之记录总结一下遇到的坑. 先说 ...
- 枚举子集&高位前缀和
最近做的题里面有这个东西,于是写一篇博客总结一下吧. 枚举子集 枚举子集就是状压的时候枚举其中的二进制位中的1的子集.直接暴力枚举二进制位时间复杂度是\(O(4^n)\),但是我们可以发现,对于每一位 ...