基础动态规划题目基础动态规划题目

目录

题目1: P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

 代码示例:

题目2: Common Subsequence

代码示例

题目3 :最长上升子序列

最长不下降子序列

最长上升子序列oj答案

题目1: P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P1216

 代码示例:

// c++ 代码示例
#include <algorithm>
#include <iostream>

using namespace std ;

int n,a[1005][1005],f[1005][1005] ;

int dfs(int x, int y)
{
    if (x == n) return a[x][y] ;
    if (f[x][y] != -1) return f[x][y] ;
    return f[x][y] = max(dfs(x + 1, y), dfs(x + 1, y + 1)) + a[x][y] ;
}

int main()
{
    int n ;
    cin >> n ;
    for (int i = 1 ; i <= n ; i++)
    {
        for (int j = 1 ; j <= i ; j++)
        {
            cin >> a[i][j] ;
        }
    }
    for (int i = 1 ; i <= n ; i++)
    {
        for (int j = 1 ; j <= i ; j++)
        {
            f[i][j] = -1 ;
        }
    }
    cout << dfs(1, 1) ;
    return 0 ;
}
// c++ 代码示例

#include <algorithm>
#include <iostream>
using namespace std ;

long long n, a[1005][1005] ;
int main()
{
    cin >> n ;
    for (int i = 1 ; i <= n ; i++)
    {
        for (int j = 1 ; j <= i ; j++)
        {
            cin >> a[i][j] ;
        }
    }
    for (int i = n ; i >= 1 ; i--)
    {
        for (int j = 1 ; j <= i ; j++)
        {
            a[i][j] = a[i][j] + max(a[i + 1][j], a[i + 1][j + 1]);        
            
        }
    }
    cout << a[1][1] ;
    return 0 ;
    
}

 

// c++ 代码示例

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;

#define rint register int
inline void read(int &x)
{
    x = 0;
    int w = 1;
    char ch = getchar();
    while (!isdigit(ch) && ch != '-')
    {
        ch = getchar();
    }
    if (ch == '-')
    {
        w = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ '0');
        ch = getchar();
    }
    x = x * w;
}

const int maxn = 1000 + 10;

int n, a[maxn][maxn], ans;

int main()
{
    read(n);
    for (rint i = 1; i <= n; i++)
    {
        for (rint j = 1; j <= i; j++)
        {
            read(a[i][j]);
            if (i == 1 && j == 1)
            {
                // The top of the triangle
                continue;
            }
            if (j == 1)
            {
                // Left boundary
                a[i][j] += a[i - 1][j];
            }
            else if (j == i)
            {
                // Right boundary
                a[i][j] += a[i - 1][j - 1];
            }
            else
            {
                // Middle elements
                a[i][j] += max(a[i - 1][j - 1], a[i - 1][j]);
            }
            ans = max(ans, a[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}

题目2: Common Subsequence

Common Subsequence - HDU 1159 - Virtual Judge (vjudge.net)icon-default.png?t=N7T8https://vjudge.net/problem/HDU-1159

代码示例

// c++ 代码示例
int a[MAXN], b[MAXN], f[MAXN][MAXN] ;

int dp()
{
    for (int i = 1 ; i <= n ; i++)
    {
        for (int j = 1 ; j <= m ; j++)
        {
            if (a[i - 1] == b[j - 1])
            {
                f[i][j] = f[i - 1][j - 1] + 1 ;
            }
            else
            {
                f[i][j] = std::max(f[i - 1][j], f[i][j - 1]) ;
            }
        }
    }
    return f[n][m] ;
}
// c++ 代码示例

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
char a[1001], b[1001];
int dp[1001][1001], len1, len2;
 
void lcs(int i,int j)
{
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(a[i-1] == b[j-1])
                dp[i][j] = dp[i-1][j-1] + 1;
            else if(dp[i-1][j] > dp[i][j-1])
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = dp[i][j-1];
        }
    }
}
 
int main()
{
    while(~scanf(" %s",a))
    {
        scanf(" %s", b);
        memset(dp, 0, sizeof(dp));
        len1 = strlen(a);
        len2 = strlen(b);
        lcs(len1, len2);
        printf("%d\n", dp[len1][len2]);
    }
    return 0;
}

题目3 :最长上升子序列

信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn)icon-default.png?t=N7T8http://ybt.ssoier.cn:8088/problem_show.php?pid=1281

最长不下降子序列

//c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a[1001] ;
int f[1001] ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
        scanf("%d", &a[i]) ;
        f[i] = 1 ;
    }
    for (int i = 2 ; i <= n ; i++)
    {
        for (int j = i - 1 ; j >= 1 ; j--)
        {
            if (a[i] >= a[j])
            {
                f[i] = max(f[i], f[j] + 1) ;
            }
        }
    }
    for (int i = 1 ; i <= n ; i++)
    {
        mx = max(mx, f[i]) ;
    }
    printf("%d", mx) ;
    return 0 ;
    
}
//c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a[1001] ;
int f[1001] ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
        scanf("%d", &a[i]) ;
        f[i] = 1 ;
    }
    for (int i = n - 1 ; i >= 1 ; i--)
    {
        for (int j = i + 1 ; j <= n ; j++)
        {
            if (a[i] <= a[j])
            {
                f[i] = max(f[i], f[j] + 1) ;
            }
        }
    }
    for (int i = 1 ; i <= n ; i++)
    {
        mx = max(mx, f[i]) ;
    }
    printf("%d", mx) ;
    return 0 ;
    
}

最长上升子序列oj答案

//c++代码示例
# include <iostream>
# include <cstdio>
using namespace std ;
int n ;
int a[1001] ;
int f[1001] ;
int mx = -1 ;
int main()
{
    scanf("%d", &n) ;
    for (int i = 1 ; i <= n ; i++)
    {
        scanf("%d", &a[i]) ;
        f[i] = 1 ;
    }
    for (int i = n - 1 ; i >= 1 ; i--)
    {
        for (int j = i + 1 ; j <= n ; j++)
        {
            if (a[i] < a[j])
            {
                f[i] = max(f[i], f[j] + 1) ;
            }
        }
    }
    for (int i = 1 ; i <= n ; i++)
    {
        mx = max(mx, f[i]) ;
    }
    printf("%d", mx) ;
    return 0 ;
    
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/803392.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET Core中创建中间件的几种方式

前言 今天我们一起来盘点一下在ASP.NET Core应用程序中添加和创建中间件常见的四种方式。 中间件介绍 ASP.NET Core中间件&#xff08;Middleware&#xff09;是用于处理HTTP请求和响应的组件&#xff0c;它们被安排在请求处理管道中&#xff0c;并按顺序执行。中间件的设计是为…

15- 微分方程

对三角函数不敏感

快捷:通过胶水语言实现工作中测试流程并行、加速

通过胶水语言实现工作中测试流程并行、加速 通过胶水语言实现工作中测试流程并行、加速工作场景&#xff08;背景&#xff09;问题抽象&#xff08;挑战&#xff09;如何做&#xff08;行动&#xff09;获得了什么&#xff08;结果&#xff09;后记相关资源 通过胶水语言实现工…

十大排序 之 快速排序

&#xff01;&#xff01;&#xff01;排序仅针对于数组哦本次排序是按照升序来的哦代码后边有图解哦 介绍 快速排序英文名为Quick Sort 基本思路 快速排序采用的是分治思想&#xff0c;即在一个无序的序列中选取一个任意的基准元素base&#xff0c;利用base将待排序的序列分…

【BUG】已解决:error: subprocess-exited-with-error

已解决&#xff1a;error: subprocess-exited-with-error 欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 欢迎来到我的主页&#xff0c;我是博主英杰&#xff0c;211科班出身&#xff0c;就职于医疗科技公司&#xff0c;热衷分享知识&#xff0c;武汉城市开发者社区主…

Ubuntu上安装配置samba服务

Ubuntu上安装配置samba服务 在Ubuntu中安装配置samba共享服务&#xff0c;可以让你在网络上共享文件和打印机。以下是一个相对详细的步骤指南&#xff0c;介绍如何在Ubuntu上安装和配置Samba。 1. 安装Samba 首先&#xff0c;需要安装Samba软件包。打开终端并运行以下命令&a…

Python基础语法篇(上)

Python基础语法&#xff08;上&#xff09; 一、基知二、基本数据类型&#xff08;一&#xff09;标准数据类型&#xff08;二&#xff09;数据类型转换 三、字符串基本操作&#xff08;一&#xff09;字符串的索引和切片&#xff08;二&#xff09;字符串的拼接 三、运算符四、…

新一代大语言模型 GPT-5 对工作与生活的影响及应对策略

文章目录 &#x1f4d2;一、引言 &#x1f4d2;二、GPT-5 的发展背景 &#x1f680;&#xff08;一&#xff09;GPT-4 的表现与特点 &#x1f680;&#xff08;二&#xff09;GPT-5 的预期进步 &#x1f4d2;三、GPT-5 对工作的影响 &#x1f680;&#xff08;一&#xf…

STM32使用Wifi连接阿里云

目录 1 实现功能 2 器件 3 AT指令 4 阿里云配置 4.1 打开阿里云 4.2 创建产品 4.3 添加设备 5 STM32配置 5.1 基础参数 5.2 功能定义 6 STM32代码 本文主要是记述一下&#xff0c;如何使用阿里云物联网平台&#xff0c;创建一个简单的远程控制小灯示例。 完整工程&a…

元宇宙深入解析

元宇宙&#xff08;Metaverse&#xff09;是一个新兴的概念&#xff0c;它激发了技术专家、艺术家和商业领袖的无限想象。它代表着数字互动的新前沿&#xff0c;提供了一个平行的数字宇宙&#xff0c;用户可以在其中实时互动&#xff0c;超越物理世界的限制。 元宇宙是什么&am…

【java】力扣 合法分割的最小下标

文章目录 题目链接题目描述思路代码 题目链接 2780.合法分割的最小下标 题目描述 思路 这道题是摩尔算法的一种扩展 我们先可以找到候选人出来&#xff0c;然后去计算他在左右两边元素出现的次数&#xff0c;只有当他左边时&#xff0c;左边出现的次数2 >左边的长度&…

【C++】拷贝构造函数及析构函数

&#x1f4e2;博客主页&#xff1a;https://blog.csdn.net/2301_779549673 &#x1f4e2;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01; &#x1f4e2;本文由 JohnKi 原创&#xff0c;首发于 CSDN&#x1f649; &#x1f4e2;未来很长&#…

百分点科技签约潍坊市数据产业发展战略合作

近日&#xff0c;潍坊市数据产业发展战略合作签约仪式举行&#xff0c;潍坊市人民政府副市长张震生&#xff0c;潍坊市财政局党组书记、局长王金祥&#xff0c;潍坊市大数据局党组书记陈强出席大会并致辞。百分点科技受邀进行战略合作签约&#xff0c;共同见证潍坊市数据要素市…

力扣—长度最小的子数组

文章目录 题目解析解题思路代码实现 题目解析 给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl, numsl1, …, numsr-1, numsr] &#xff0c;并返回其长度。如果不存在符合条件的子数组&#xff0c;返回…

捷配笔记-HDI板与普通PCB板的区别

HDI是什么? HDI(High Density Interconnect) 全称高密度互连板&#xff0c;是一种线分布密度高的高密度电路板&#xff0c;在现代电子设备中扮演着至关重要的角色。 它具有轻薄、线路密度高、有利于先进构装技术的使用、电气特性与信号更佳、改善射频干扰/电磁波干扰/静电释放…

matlab PID tuner整定工具箱的用法

从主页的APP中搜索到它&#xff1a; 按照下图IMPORT导入被控对象的传递函数 在下图的Inspect按钮中可以看到导入的被控对象的传函。 在下图的Type中选择控制器类型&#xff1a; 在下图的Form中选择PID的形式&#xff1a;有两种可选&#xff1a;平行式Parallel和标准式Standard …

K8S 上部署 Emqx

文章目录 安装方式一&#xff1a;快速部署安装方式二&#xff1a;定制化部署1. 使用 Pod 直接部署 EMQX Broker2. 使用 Deoloyment 部署 Pod3. 使用 Services 公开 EMQX Broker Pod 服务4. 通过 kubernetes 自动集群 EMQX MQTT 服务器5. 修改 EMQX Broker 的配置6. 赋予 Pod 访…

阿里云短信PHP集成api类

无需安装sdk扩展包&#xff0c;直接引入类即可使用 V3版本请求体&签名机制:自研请求体和签名机制 - 阿里云SDK - 阿里云 模版内容&#xff1a; <?phpnamespace common\components;use common\constant\UserConst; use common\models\bee\SmsReferer; use common\mode…

【VScode】安装【ESP-IDF】插件及相关工具链

一、ESP-IDF简介 二、VScode安装ESP-IDF插件 三、安装ESP-IDF、ESP-IDF-Tools以及相关工具链 四、测试例程&编译烧录 一、ESP-IDF简介 二、VScode安装ESP-IDF插件 【VScode】安装配置、插件及远程SSH连接 【VSCode】自定义配置 打开VScode&#xff0c;在插件管理搜索esp…

【程序大侠传】服务发布引发mq消息重复消费

前序 在编程武侠世界中&#xff0c;有一个门派“天机楼”&#xff0c;连接并协调各大门派之间的关系&#xff0c;确保整个江湖的运作流畅无阻。天机楼住要的业务范围主要如下&#xff1a; 信息传递的信使&#xff1a; 天机楼就像是江湖中的飞鸽传书&#xff0c;确保各门派之间…