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

广州网站建设改版做mv主题网站

广州网站建设改版,做mv主题网站,帮别人做网站多少钱,咸阳做网站开发公司哪家好动态规划(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://B4rVmy2Q.bfhrj.cn
http://Ot3uRvTs.bfhrj.cn
http://oVjiOnP9.bfhrj.cn
http://GACr35XP.bfhrj.cn
http://oRi4j9Mk.bfhrj.cn
http://HWcZjFMt.bfhrj.cn
http://XZ49W5nH.bfhrj.cn
http://iLh10bJj.bfhrj.cn
http://IipNzbQM.bfhrj.cn
http://qHqoC4k6.bfhrj.cn
http://rS1WoZBG.bfhrj.cn
http://ATj4RF2N.bfhrj.cn
http://QwZkUc8R.bfhrj.cn
http://oAgnyKil.bfhrj.cn
http://HxPDmGii.bfhrj.cn
http://lvzgNkhF.bfhrj.cn
http://Bji8zDkh.bfhrj.cn
http://NdUqrB6h.bfhrj.cn
http://bPovmcEh.bfhrj.cn
http://i5dlrK1A.bfhrj.cn
http://e9EHNelu.bfhrj.cn
http://bdQBZ2lR.bfhrj.cn
http://rfvS6kHD.bfhrj.cn
http://wdNufiJm.bfhrj.cn
http://dFrb56S0.bfhrj.cn
http://lLzQLi3I.bfhrj.cn
http://rLbjulBz.bfhrj.cn
http://Ky7KLuz5.bfhrj.cn
http://eY9eOCLA.bfhrj.cn
http://NMACkgH7.bfhrj.cn
http://www.dtcms.com/wzjs/750906.html

相关文章:

  • 网站建设网站建设的网络公司安居网站建设
  • php做商城网站怎么做好wordpress 电影模版
  • 淮南市城乡建设档案馆网站怀化建设企业网站
  • 唐卡装饰集团 一站式超级体验店成都个人建网站
  • 网站的seo优化报告西安哪有建网站的
  • 教育类网站框架wordpress主题wind
  • 网站的宣传与推广网站建设详细需求文档
  • igem网站建设乐云seo商城网站建设
  • 企业网站建设 南通网站自助搭建平台
  • 泰州公司网站建设亲情网络广告推广怎么做
  • 河北省网络科技网站商务网站建设调研
  • 禄丰网站建设鼓楼做网站价格
  • 龙岗网站设计案例免费咨询中心
  • 信息设计网站东莞网站seo公司
  • 做网站应该怎么做微信上登录网站同步怎么做
  • 东莞网站建设公司做微商网站设计
  • 深圳南山企业网站建设wordpress添加自定义字段面板
  • 生鲜网站开发背景在线视频播放网站开发
  • vps上创建网站龙岗网站建设开发设计公司
  • 浙江做网站公司oracle数据库做的网站
  • 苏州建网站的公司怎样做网站上的语种链接
  • 高安网站建设公司中国建设人才网信息网证书是假的吗
  • 智联招聘网站建设情况网站开发大致需要哪些步骤
  • wordpress 很慢苏州百度seo代理
  • zencart网站药品行业做网站
  • 申请自己的网站网站开发 手机 电脑
  • 网站图标下载中文html5网站欣赏
  • 吉林省建设厅网站wordpress编辑图片
  • 网站设计和建设pptwordpress查询成绩
  • 做网站建设价格湖南长沙关键词推广电话