LeetCode1143. Longest Common Subsequence——动态规划

news/2024/10/23 3:40:40/

文章目录

    • 一、题目
    • 二、题解

一、题目

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

For example, “ace” is a subsequence of “abcde”.
A common subsequence of two strings is a subsequence that is common to both strings.

Example 1:

Input: text1 = “abcde”, text2 = “ace”
Output: 3
Explanation: The longest common subsequence is “ace” and its length is 3.
Example 2:

Input: text1 = “abc”, text2 = “abc”
Output: 3
Explanation: The longest common subsequence is “abc” and its length is 3.
Example 3:

Input: text1 = “abc”, text2 = “def”
Output: 0
Explanation: There is no such common subsequence, so the result is 0.

Constraints:

1 <= text1.length, text2.length <= 1000
text1 and text2 consist of only lowercase English characters.

二、题解

class Solution {
public:int longestCommonSubsequence(string text1, string text2) {int n = text1.size(),m = text2.size();vector<vector<int>> dp(n+1,vector<int>(m+1,0));for(int i = 1;i <= n;i++){for(int j = 1;j <= m;j++){if(text1[i - 1] == text2[j - 1]) dp[i][j] = 1 + dp[i-1][j-1];else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);}}return dp[n][m];}
};

http://www.ppmy.cn/news/1350048.html

相关文章

C++之多线程(multi-thread)

理论基础 多线程编程是C中一个重要而复杂的主题。下面是一些建议和步骤&#xff0c;帮助你入门多线程编程&#xff1a; 了解基础概念&#xff1a; 线程和进程&#xff1a; 理解线程和进程的基本概念。 并发和并行&#xff1a; 区分并发和并行的概念&#xff0c;了解它们在多线…

没更新的日子也在努力呀,布局2024!

文章目录 ⭐ 没更新的日子也在努力呀⭐ 近期的一个状态 - 已圆满⭐ 又到了2024的许愿时间了⭐ 开发者要如何去 "创富" ⭐ 没更新的日子也在努力呀 感觉很久没有更新视频了&#xff0c;好吧&#xff0c;其实真的很久没有更新短视频了。最近的一两个月真的太忙了&#…

第一章、APPium、android自动化

第一章、APPium、android自动化 第二章、android利用QPython环境调用APPium 环境搭建 1、android studio 配置SDK Manger 的sdk、ndk、adb 控制台输入 open -e /Users/shenjianbin/.bash_profile.save export ANDROID_SDK/Users/shenjianbin/Library/Android/sdk export PA…

OCP使用web console创建和构建应用

文章目录 环境登录创建project赋予查看权限部署第一个image检查pod扩展应用 部署一个Python应用连接数据库创建secret加载数据并显示国家公园地图 清理参考 环境 RHEL 9.3Red Hat OpenShift Local 2.32 登录 在 crc start 启动crc时&#xff0c;可以看到&#xff1a; .....…

ClickHouse--02--安装

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 安装官网 &#xff1b;[https://clickhouse.com/docs/zh/getting-started/install](https://clickhouse.com/docs/zh/getting-started/install)![在这里插入图片描述…

中国电子学会2020年9月份青少年软件编程Scratch图形化等级考试试卷三级真题(编程题)

编程题(共3题&#xff0c;共30分) 36.题目&#xff1a;魔术表演“开花” 1.准备工作 &#xff08;1&#xff09;将舞台设置为"Party"&#xff1b; &#xff08;2&#xff09;删除默认角色&#xff0c;自行绘制椭圆花瓣角色&#xff1b; &#xff08;3&#xf…

C++ | KMP算法模板

next数组初始化 char a[1000006];//原串 char p[1000006];//子串 int pmt[1000006];void getNext(int m){int j0;pmt[0]0;for(int i1;i<m;i){while(j>0 && p[i]!p[j])jpmt[j-1];if(p[i]p[j])j;pmt[i]j;} }以下实例基于上述getNext函数及数据结构执行&#xff1a…

Flink从入门到实践(三):数据实时采集 - Flink MySQL CDC

文章目录 系列文章索引一、概述1、版本匹配2、导包 二、编码实现1、基本使用2、更多配置3、自定义序列化器4、Flink SQL方式 三、踩坑1、The MySQL server has a timezone offset (0 seconds ahead of UTC) which does not match the configured timezone Asia/Shanghai. 参考资…