子2023

【问题描述】
小蓝在黑板上连续写下从 1 到 2023 之间所有的整数,得到了一个数字序列:
S = 12345678910111213 . . . 20222023。
小蓝想知道 S 中有多少种子序列恰好等于 2023?
提示,以下是 3 种满足条件的子序列(用中括号标识出的数字是子序列包含的数字):
1[2]34567891[0]111[2]1[3]14151617181920212223...
1[2]34567891[0]111[2]131415161718192021222[3]...
1[2]34567891[0]111213141516171819[2]021222[3]...
注意以下是不满足条件的子序列,虽然包含了 2、0、2、3 四个数字,但是顺序不对:
1[2]345678910111[2]131415161718192[0]21222[3]...

1.dp写法

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long LL;
typedef pair<int,int> PII;
string s;
LL dp[5];

int main()
{
 	IOS;
	
	for(int i=1;i <= 2023;i++)	s+=to_string(i);

	for(int i=0;i < s.size();i++)
	{
		if(s[i] == '2')
		{
			dp[1]++;
			dp[3]+=dp[2];
		}
		
		if(s[i] == '0') dp[2]+=dp[1];
		if(s[i] == '3') dp[4]+=dp[3];
	}
		
	cout<<dp[4];
	
 	return 0;
}

2.剪枝后暴力

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long LL;
typedef pair<int,int> PII;
string s;
LL ans=0;
string op="2023";

void dfs(int u,int cnt)
{
	if(cnt == 4)
	{
		ans++;
		return ;
	}
	if(u >= s.size()) return ;
	
	if(op[cnt] == s[u]) dfs(u+1,cnt+1);
	dfs(u+1,cnt);
}

int main()
{
 	IOS;
	
	for(int i=1;i <= 2023;i++)
	{
		string t=to_string(i);
		for(int j=0;j < t.size();j++)
		{
			if(t[j] == '2' || t[j] == '0' || t[j] == '3')
			s+=t[j];	
		}	
	} 	
	
	dfs(0,0);
	
	cout<<ans<<endl;
	
 	return 0;
}

枚举四个位置,只有数字放到该放的位置,才能填下一个位置,也可以选择不填。

相关推荐
汉克老师5 分钟前
GESP5级C++考试语法知识(十六、分治算法(三))
c++·算法·分治算法·汉诺塔·逆序对·gesp5级·gesp五级
V搜xhliang02468 分钟前
OpenClaw进阶完全教程
运维·人工智能·算法·microsoft·自动化
hele_two10 分钟前
SDL2设置透明度
c++·图形渲染
小杰31210 分钟前
网络框架源码阅读技巧
服务器·网络·c++·reactor·zlmediakit·zltoolkit
叼烟扛炮15 分钟前
C++ 知识点12 构造函数
开发语言·c++·算法·构造函数
满天星830357727 分钟前
定长内存池ObjectPool
数据结构·c++·算法·链表
叼烟扛炮30 分钟前
C++第八讲:string 类
开发语言·c++·算法·string
MOONICK33 分钟前
bit7z压缩与解压
c++
Chase_______38 分钟前
LeetCode 1493 & 3634 题解:滑动窗口双指针,从“删一个元素的全1子数组“到“最少移除使数组平衡“
算法·leetcode
努力努力再努力wz43 分钟前
【Qt入门系列】第一个 Qt Widgets 程序:项目创建、UI 文件、Hello World、对象树与 qDebug 日志
java·c语言·开发语言·数据结构·c++·qt·ui