当前位置:网站首页>【代码随想录-动态规划】最长公共子序列
【代码随想录-动态规划】最长公共子序列
2022-06-29 04:30:00 【不写博客就不爽】
T1143、最长公共子序列
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
若这两个字符串没有公共子序列,则返回 0。
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度
这个题目变种可以变成522. 最长特殊序列 II
判断一个字符串 s 是否是另一个字符串 t 的子序列
就是当最长公共子序列 == s.length() 时, s ∈ t s \in t s∈t
思路
step1、dp[i][j] 表示 text1[0:i-1] 和 text2[0:j-1] 的最长子序列长度是 dp[i][j]
step2、
当text1[i-1] == text2[j-1]时
dp[i][j] = dp[i-1][j-1] + 1
当text1[i-1] != text2[j-1]时
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
private boolean check(String a, String b){
// 判断a 是否 是 b的子序列
// a 是源串 b是可改变串
int l1 = a.length();
int l2 = b.length();
if(l1 > l2) return false;
int[][] dp = new int[l1+1][l2+1];
for(int i = 1; i <= l1; i++){
for(int j = 1; j <= l2; j++){
if(a.charAt(i-1) == b.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
if(dp[l1][l2] == l1) return true;
return false;
}
T522. 最长特殊序列 II
public int findLUSlength(String[] strs) {
int n = strs.length;
// 判断一个串是否是其他的子串
int ret = -1;
for (int i = 0; i < n; i++) {
if (strs[i].length() <= ret) continue;
boolean ok = true;
for (int j = 0; j < n && ok; j++) {
if (i == j) continue;
if (check(strs[i], strs[j])) ok = false;
}
if (ok) ret = strs[i].length();
}
return ret;
}
边栏推荐
- Force deduction solution summary 324- swing sequencing II
- 项目开发修养
- Remediation for Unsafe Cryptographic Encryption
- c语言 --- 分支结构
- The 30th day of force deduction (DP topic)
- How to create a subtype like relationship between two generic classes when the classes are generic related
- SQL two columns become multi row filter display
- 波形记录仪MR6000的实时波形运算功能
- Log in to the MySQL database and view the version number on the command line
- Decorator Pattern
猜你喜欢

Daily practice - February 15, 2022

ROS TF coordinate transformation Library & rewrite to create high frequency coordinate transformation broadcaster

Mediator pattern

What are the circular statements of MySQL
![[new function] ambire wallet integrates Metis network](/img/29/8a8c0cd40c51cef1174ee59706d4c9.png)
[new function] ambire wallet integrates Metis network

Analysis of moudo Network Library

The 30th day of force deduction (DP topic)

I came from a major, so I didn't want to outsource

Decorator Pattern

Live broadcast appointment AWS data everywhere series activities
随机推荐
Remediation for Unsafe Cryptographic Encryption
[wc2021] Fibonacci - number theory, Fibonacci sequence
Is the interviewer too difficult to serve? A try catch asks so many tricks
[hackthebox] dancing (SMB)
[C language] explain the thread recycling function pthread_ join
MySQL column to row conversion without Union
Wi-Fi 7 来啦,它到底有多强?
How to quickly install MySQL 5.7.17 under CentOS 6.5
Error accessing database
Build a simple website by yourself
Redis cache penetration, cache breakdown, cache avalanche
如何创建 robots.txt 文件?
Seattention channel attention mechanism
SEAttention 通道注意力機制
ECS 四 Sync Point、Write Group、Version Number
iNFTnews | 元宇宙技术将带来全新的购物体验
I came from a major, so I didn't want to outsource
Quelles sont les méthodes de simulation et de gravure des programmes? (comprend les outils communs et la façon dont ils sont utilisés)
Five thousand years of China
【Laravel系列8】走出 Laravel 的世界