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 ...
随机推荐
- Sparsity稀疏编码(三)
稀疏编码(sparse coding)和低秩矩阵(low rank)的区别 上两个小结介绍了稀疏编码的生命科学解释,也给出一些稀疏编码模型的原型(比如LASSO),稀疏编码之前的探讨文章 ...
- java基础语法 数组
数组是相同数据类型元素的集合 数组本身是引用数据类型,即对象.但是数组可以存储基本数据类型,也可以存储引用数据类型. 在java中如果想要保存一组基本类型的数据,使用数组:如果想保存一组对象或者其 ...
- 运行.xcworkspace项目后报错:'React/RCTBundleURLProvider.h’ file not found
情况:根据https://github.com/rebeccahughes/react-native-device-info添加依赖库,运行.xcworkspacea项目后报错 解决:Delete n ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- ng-深度学习-课程笔记-6: 建立你的机器学习应用(Week1)
1 训练/验证/测试集( Train/Dev/test sets ) 构建神经网络的时候有些参数需要选择,比如层数,单元数,学习率,激活函数.这些参数可以通过在验证集上的表现好坏来进行选择. 前几年机 ...
- Git冲突:commit your changes or stash them before you can merge. 解决办法
用git pull来更新代码的时候,遇到了下面的问题: 1 2 3 4 error: Your local changes to the following files would be overwr ...
- Sybase IQ使用过程中注意事项
Sybase IQ使用过程中注意事项 1,字母大小写比对不敏感,也就是在值比对判断时大小写字母都一样; 2,等值,或<>判断,系统默认对等式两边比对值去右边空格再进行比较: 3,GROUP ...
- 2018-2019-1 20189215 《Linux内核原理与分析》第七周作业
<庖丁解牛>第六章书本知识总结 操作系统内个实现操作系统的三大管理功能:进程管理.内存管理.文件系统.分别对应<操作系统原理>中最重要的3个抽象概念是进程.虚拟内存和文件. L ...
- 20145326《Java程序设计》第一周学习总结
20145326<Java程序设计>第一周学习总结 教材学习内容总结 转眼间新的一学期终于开始了!为什么我这么期待呢?因为这学期可以上娄嘉鹏老师的java程序设计课,我不是什么电脑天才,之 ...
- How does flyway sort version numbers?
https://stackoverflow.com/questions/19984397/how-does-flyway-sort-version-numbers In one word: numer ...