当前位置:网站首页>762 · longest common subsequence II
762 · longest common subsequence II
2022-04-22 07:31:00 【yinhua405】
describe
Given two sequences P and Q .
You can treat this pair of P This sequence cannot be modified more than k An element to any value , The longest common subsequence of the two modified sequences is required to be the longest .
Wechat plus jiuzhang15 Send verification information 【 Big factories in China 】 Collar byte 、 Ali 、 Baidu and other latest high-frequency questions
Examples
Examples 1:
Input :
[8,3]
[1,3]
1
Output :
2
explain :
hold 8 become 1, The common subsequence is [1,3]
Examples 2:
Input :
[1, 2, 3, 4, 5]
[5, 3, 1, 4, 2]
1
Output :
3
int longestCommonSubsequence2(vector<int> &P, vector<int> &Q, int k)
{
int lenP = P.size(), lenQ = Q.size();
vector<vector<vector<int>>> dp(lenP + 1, vector<vector<int>>(lenQ + 1, vector<int>(k + 1, 0)));
for (int i = 1; i <= lenP; i++)
{
for (int j = 1; j <= lenQ; j++)
{
for (int m = 0; m <= k; m++)
{
if (P[i - 1] == Q[j - 1])
{
dp[i][j][m] = dp[i - 1][j - 1][m] + 1;
}
else
{
if (m == 0)
{
dp[i][j][m] = max(dp[i][j - 1][m], dp[i - 1][j][m]);
}
else
{
dp[i][j][m] = max(max(dp[i - 1][j - 1][m - 1] + 1, dp[i][j - 1][m]), dp[i - 1][j][m]);
}
}
}
}
}
return dp[lenP][lenQ][k];
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324507.html
边栏推荐
- 762 · 最长公共子序列 II
- L1-071 previous life files (20 points) (similar two points)
- Leetcode - 4 - (longest substring without repeated characters, candy distribution, binary tree traversal)
- HDU Ice_cream‘s world I (并查集判环)
- 最小圆覆盖(计算几何基础)
- A. Alice and Bob (博弈?思维&暴力)(2021牛客暑期多校训练营1)
- H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )
- Codeforces Round #588 (Div. 2) C D
- Installation and configuration of Yapi (Reprint)
- Codeforces Round #779 (Div. 2)
猜你喜欢

A. Alice and Bob (博弈?思维&暴力)(2021牛客暑期多校训练营1)

Leetcode - 6 - (string multiplication, next larger element < Ⅰ Ⅱ Ⅲ >, K sets of inverted linked list)

D. Determine the photo position (simply find the substring) (2021 Niuke summer multi school training camp 1)

并发编程的艺术(11):JUC里的工具类介绍

Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)

HDU Ice_cream‘s world I (并查集判环)

A.Binary Seating (概率) (2021年度训练联盟热身训练赛第五场)

1005 monopoly congruence solution (2021 Chinese college student programming competition CCPC network trial replay)

L1-071 previous life files (20 points) (similar two points)

L1-064 AI core code valued at 100 million (20 points) has wrong format
随机推荐
If I make this silly mistake again/ (ㄒoㄒ)/~~
LeetCode - 1 - (树的子结构、组合、螺旋矩阵、全排列<ⅠⅢ>)
并发编程的艺术(3):深入理解Synchronized的原理
119 · 编辑距离
323 · 字符串游戏
437. Path sum III
278 · draw fill
Win10 Anaconda install cocotb
119 · edit distance
1420 · 最小覆盖子串II
Why is the data stored in the leaf node of the non primary key index the primary key value
332 · 恢复数组
重写与重载的定义与区别
C language | pointer
Longest ascending sequence
递归找序列集合
363 · 接雨水
pip一直更新失败?多数是网络问题!
L2-004 这是二叉搜索树吗?(先序输入&判断搜索二叉树&后序输出)
L1-071 前世档案 (20 分) (类似二分)