当前位置: 首页 > wzjs >正文

黄石网站建设黄石搭建本地网站

黄石网站建设黄石,搭建本地网站,美丽阿坝网站怎么做,企业网站建站模板动态规划(Dynamic Programming, DP)在字符串数组相关的算法题中应用广泛,尤其是在解决子序列、子串、编辑距离、匹配等问题时。动态规划的核心思想是将问题分解为子问题,并通过存储子问题的解来避免重复计算,从而提高效…

动态规划(Dynamic Programming, DP)在字符串数组相关的算法题中应用广泛,尤其是在解决子序列、子串、编辑距离、匹配等问题时。动态规划的核心思想是将问题分解为子问题,并通过存储子问题的解来避免重复计算,从而提高效率。

文章目录

  • 1143. Longest Common Subsequence
    • 问题描述
    • 解题思路
    • C++ 实现
  • 1092. Shortest Common Supersequence
    • 解题思路
    • C++ 实现

1143. Longest Common Subsequence

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.

问题描述

给定两个字符串 s1s2,找到它们的最长公共子序列的长度。子序列是指从原字符串中删除一些字符(可以不连续)后得到的新字符串。

解题思路

  • 定义状态dp[i][j] 表示 s1 的前 i 个字符和 s2 的前 j 个字符的最长公共子序列长度。
  • 状态转移
    • 如果 s1[i-1] == s2[j-1],则 dp[i][j] = dp[i-1][j-1] + 1
    • 否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1])
  • 初始化dp[0][j] = 0dp[i][0] = 0,表示空字符串的 LCS 长度为 0。
  • 结果dp[m][n],其中 mn 分别是 s1s2 的长度。

C++ 实现

int longestCommonSubsequence(string s1, string s2) {int m = s1.length(), n = s2.length();vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (s1[i - 1] == s2[j - 1]) {dp[i][j] = dp[i - 1][j - 1] + 1;} else {dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);}}}return dp[m][n];
}

1092. Shortest Common Supersequence

Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.

A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.

Example 1:

Input: str1 = "abac", str2 = "cab"
Output: "cabac"
Explanation: 
str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.

Example 2:

Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
Output: "aaaaaaaa"

解题思路

这个问题可以转化为求两个字符串的最长公共子序列(LCS),然后通过 LCS 构造最短的公共超序列。

构造最短公共超序列

  • 初始化指针:
    • 使用指针 ij 分别遍历 str1str2
  • 遍历字符串:
    • 如果 str1[i] == str2[j],则将当前字符添加到结果中,并移动两个指针。
    • 否则,将 str1[i]str2[j] 中较小的字符添加到结果中,并移动对应的指针。
  • 处理剩余字符:
    • 如果 str1str2 有剩余字符,将它们全部添加到结果中。

C++ 实现

string shortestCommonSupersequence(string str1, string str2) {int m = str1.length(), n = str2.length();// 动态规划求 LCSvector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (str1[i - 1] == str2[j - 1]) {dp[i][j] = dp[i - 1][j - 1] + 1;} else {dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);}}}// 构造最短公共超序列string result;int i = m, j = n;while (i > 0 && j > 0) {if (str1[i - 1] == str2[j - 1]) {result.push_back(str1[i - 1]);i--;j--;} else if (dp[i - 1][j] > dp[i][j - 1]) {result.push_back(str1[i - 1]);i--;} else {result.push_back(str2[j - 1]);j--;}}// 处理剩余字符while (i > 0) {result.push_back(str1[i - 1]);i--;}while (j > 0) {result.push_back(str2[j - 1]);j--;}// 反转结果reverse(result.begin(), result.end());return result;
}
http://www.dtcms.com/wzjs/548267.html

相关文章:

  • 地方网站欣赏百度收录情况查询
  • 中国网站建设总部在哪里鞍山互动网
  • 专门做焦点图的网站美篇相册制作免费下载app
  • 贷款公司如何做网站中信建设有限责任公司是上市公司吗
  • 大港油田建设网站企业综合查询网站
  • 网站开发选题申请理由网络广告营销实现方式解读
  • 西安火车站建设搜索排行榜
  • 信息网站方案怎么玩互联网能赚钱
  • 做网站改版的wordpress+主题页脚
  • 赣州网站建设费用网站后台建设费用
  • 怎么建立公司网站平台昆明 做网站 vr
  • 电脑网站安全证书有问题如何解决网络游戏的利弊
  • 成都网站建设优化推广州安全教育平台app下载
  • tor网站建设网站侧边栏模板
  • 如何看一个关键词在某个网站是否被百度收录聚财洋气三个字公司名字
  • 给别人做网站能赚钱吗公司网站制作门槛
  • 无锡seo网站推广费用传统企业网站建设
  • 建设网站花费网站建设 费用 入哪个科目
  • 网站建设叁金手指花总7html5网站开发实例教程
  • 企业网站建设找外包公司做做网站要多少钱 知乎
  • 上海培训网站建设苏州网站建设公司鹅鹅鹅
  • 男女明星直接做的视频网站国家建设厅网站
  • 上海网站制作开发公司网站备案信息注销原因
  • 网站修改域名服务器官方网站如何做
  • 免费的免抠图素材网站个人养老保险怎么买最划算
  • 网站开发所需开发环境网站模板之家官网
  • 网站建设费入如保入账静态网站什么样
  • 外贸专业网站盐山网站制作
  • 婚庆网站建设论文企业网站开发价钱低
  • 阜阳市重点工程建设局网站社交网站开发技术岗