LightOJ 1205 Palindromic Numbers(数位DP 回文数)

news/2024/7/4 1:44:45

Description 
求[a,b]中回文数的个数 
Input 
第一行为用例组数t,之后t行每行两个整数a和b表示查询区间端点 
Output 
对于每组用例,输出区间[a,b]中回文数的个数 
Sample Input 

1 10 
100 1 
1 1000 
1 10000 
Sample Output 
Case 1: 9 
Case 2: 18 
Case 3: 108 
Case 4: 198 
Solution 
简单数位DP,开一个数组保存确实巧妙

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[30],b[30];
ll dp[30][30][30];
ll dfs(int pos,int s,bool sta,int lead,int limit)
//s表示回文串的起点,pos表示正在搜索的位,sta表示目前构成的串是否为回文串
{
	if(pos==-1){
		return sta;
	}
	if(!limit&&!lead&&dp[pos][s][sta]!=-1) 
	return dp[pos][s][sta];
	int up=limit?a[pos]:9;
	ll tmp=0;
	for(int i=0;i<=up;i++)
	{
	b[pos]=i;
	if(lead==1&&i==0)     //前导0
	{
	tmp+=dfs(pos-1,s-1,sta,lead,limit&&i==up);
	}
	else 
	 {                         
	 	if(sta&&pos<(s+1)/2)//草稿纸看看
	 	                    //枚举后半段
	 	tmp+=dfs(pos-1,s,b[s-pos]==i,lead && i==0,limit&&i==up);
	 	else               //填充前半段
		tmp+=dfs(pos-1,s,sta,lead && i==0,limit&&i==up);
	 }
	}
	if(!limit&&!lead) 
	dp[pos][s][sta]=tmp;
	return tmp; 
}
 
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
     
    return dfs(pos-1,pos-1,1,true,true);//注意sta一开始要初始化为1,找了好久,因为无也是个回文字符串
}
int main()
{
	
    memset(dp,-1,sizeof(dp));
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
	ll n,m;
	scanf("%lld%lld",&n,&m);
	if(n>m) swap(n,m);
	printf("Case %d: %lld\n",i,solve(m)-solve(n-1));
    }

    return 0;
}

 


http://www.niftyadmin.cn/n/3628393.html

相关文章

C# 移动第一个重复字符到前面

例如 abcdbbfg 变成 bbbacdfg&#xff0c;要求时间复杂度为N,空间复杂度为1&#xff0c;写了两个方法都未达到要求 :( View Code static void MoveDupCharToFront(char[] input){if (input null|| input.Length <1){throw new Exception("input cant be empty or les…

XenDesktop多用户不同时间使用同一个发布的物理机桌面

XenDesktop多用户不同时间使用同一个发布的物理机桌面 Citrix XenDesktop 5.5发布一台物理机的OS桌面时&#xff08;非XenServer、VMware-ESX以及Hyper-V等虚拟系统&#xff09;&#xff0c;默认情况下&#xff0c;就算在创建桌面时将这个桌面赋予多个用户&#xff0c;当其中一…

Python迭代器和生成器(改编自知乎相关文章)

Python迭代器和生成器&#xff08;改编自知乎相关文章&#xff09; 1.迭代器 有一些Python对象&#xff0c;我们可以从中按一定次序提取出其中的元素。这些对象称之为可迭代对象。比如&#xff0c;字符串、列表、元组都是可迭代对象。 我们回忆一下从可迭代对象中提取元素的过程…

第15周阅读程序(3)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<algorithm> #include<functional> #include<vector&g…

第15周阅读程序(4)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<algorithm> #include<functional> #include<iostream…

lightoj 1140 - How Many Zeroes?(数位DP)

1140 - How Many Zeroes? PDF (English)StatisticsForum Time Limit: 2 second(s)Memory Limit: 32 MB Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down? Input Inp…

JS对象继承(6种)

原型链(父类的实例)借用构造函数(调用超类构造函数)组合继承(原型链和借用构造函数)原型式继承(借助原型基于已有的对象上创建新对象)寄生式继承(创建一个仅用于封装继承过程的函数)寄生组合式继承(解决父类属性重写)ECMAScript无法实现接口继承&#xff0c;只支持实现继承&…

第15周阅读程序(5)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<iterator> #include<algorithm> #include<functional…