当前位置:网站首页>[code Capriccio - dynamic planning] longest common subsequence
[code Capriccio - dynamic planning] longest common subsequence
2022-06-29 04:34:00 【Not blogging】
T1143、 Longest common subsequence
Given two strings text1 and text2, Returns the length of the longest common subsequence of these two strings .
A string of Subsequence This is a new string : It is to delete some characters from the original string without changing the relative order of the characters ( You can also delete no characters ) After the composition of the new string .
for example ,“ace” yes “abcde” The subsequence , but “aec” No “abcde” The subsequence . Two strings of 「 Common subsequence 」 Is the subsequence shared by these two strings .
If the two strings have no common subsequence , Then return to 0.
Given two strings text1 and text2, Returns the length of the longest common subsequence of these two strings
The variant of this topic can become 522. The longest special sequence II
Judging a string s Whether it's another string t The subsequence
When the longest common subsequence == s.length() when , s ∈ t s \in t s∈t
Ideas
step1、dp[i][j] Express text1[0:i-1] and text2[0:j-1] The longest subsequence length of is dp[i][j]
step2、
When text1[i-1] == text2[j-1] when
dp[i][j] = dp[i-1][j-1] + 1
When text1[i-1] != text2[j-1] when
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
private boolean check(String a, String b){
// Judge a whether yes b The subsequence
// a Is the source string b Yes changeable string
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. The longest special sequence II
public int findLUSlength(String[] strs) {
int n = strs.length;
// Determine whether a string is another substring
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;
}
边栏推荐
- Blue Bridge Cup DFS (I)
- 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
- From zero to one, I will teach you to build a "search by text and map" search service (I)
- iNFTnews | 元宇宙技术将带来全新的购物体验
- lua-protobuff emmy-lua 轮子
- Practical part: solving the function conflict between swagger and user-defined parameter parser
- Redis 缓存穿透、缓存击穿、缓存雪崩
- NotImplementedError: Could not run torchvision::nms
- Is the interviewer too difficult to serve? A try catch asks so many tricks
- 什么是匿名内部类,如何使用匿名内部类
猜你喜欢

Here comes Wi Fi 7. How strong is it?

Introduction to Bert and Vit

Redis cache penetration, cache breakdown, cache avalanche

What is the method of connection query in MySQL

Flyweight pattern

LabVIEW显示Unicode字符

仿真與燒錄程序有哪幾種方式?(包含常用工具與使用方式)

How to quickly install MySQL 5.7.17 under CentOS 6.5

Redis 缓存穿透、缓存击穿、缓存雪崩

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
随机推荐
人民银行印发《关于支持外贸新业态跨境人民币结算的通知》
Idea modifying JVM memory
Seattention channel attention mechanism
Talking about Canary deployment
Force deduction solution summary 324- swing sequencing II
泰克TDS3054B示波器技术指标
What exactly does GCC's -Wpsabi option do? What are the implications of supressing it?
SQL database stored procedure writing method
ROS URDF model is parsed into KDL tree
1018 锤子剪刀布
What are the basic usage methods of MySQL
Real time waveform calculation function of Waveform Recorder mr6000
仿真與燒錄程序有哪幾種方式?(包含常用工具與使用方式)
ECS 四 Sync Point、Write Group、Version Number
What is the method of connection query in MySQL
Libuv library overview and comparison of libevent, libev and libuv (Reprint)
Mécanisme d'attention du canal de fixation
LabVIEW displays Unicode characters
Rapid development project -vscode plug-in
Cucumber test practice