当前位置:网站首页>Longest common subsequence (record path version)
Longest common subsequence (record path version)
2022-04-23 01:43:00 【Stick to your original heart and go to your dream】
Record path
Since we mentioned last time that we can pass ** Memory search as well as Dynamic programming and tabulation ** Get its length , But the curious thing is , Can we use some simple means , Record its path ??
The answer, of course, is Sure Of , We just need one An array of the same size Can satisfy our little wish .
Action is better than action ~~~ Please look at the code. :
Memory search
int process(string& str1, string& str2, int i, int j) {
// With i,j Subscript for the end
if (dp[i][j])// If the answer to this position has been solved , Then return directly to the answer
{
return dp[i][j];
}
if (i == 0 && j == 0) {
// Analyze various boundary conditions ( When one or both strings have only one character left , Analyze the equality and inequality of the two ), Find recursive exit
if (str1[i] == str2[j]) {
dp[i][j] = 1;
}
flag[i][j] = 1;// Record path ( If they are the same, mark them as 1, Because there is only one character left in both strings ,
// So whether it's marked as 1,2, still 3( That is, wherever you go ) It's going to end , So it's not important to mark the number here )
return dp[i][j];
}
else if (i == 0) {
if (str1[i] == str2[j]) {
dp[i][j] = 1;
flag[i][j] = 1;
return dp[i][j];
}
else {
// If different , Then marked as 2( Go to the left )
dp[i][j] = process(str1, str2, i, j - 1); flag[i][j] = 2; return dp[i][j];
}
}
else if (j == 0) {
if (str1[i] == str2[j]) {
flag[i][j] = 1;
dp[i][j] = 1; return dp[i][j];
}
else {
// Marked as 3( Go to the right )
dp[i][j] = process(str1, str2, i - 1, j); flag[i][j] = 3; return dp[i][j];
}
}
int p1 = process(str1, str2, i, j - 1);
int p2 = process(str1, str2, i - 1, j); int p3 = 0;
if (str1[i] == str2[j]) {
p3 = process(str1, str2, i - 1, j - 1) + 1;
flag[i][j] = 1;// If they are equal, mark
}
dp[i][j] = max(max(p1, p2), p3);
if (!flag[i][j]) {
// If it works here , That means it wasn't marked before , That is, the two characters are not equal , Then find the mark with the largest length in the next step
if (p2 >= p1)flag[i][j] = 3;
else flag[i][j] = 2;
}
return dp[i][j];
}
So let's say , We just need to define one flag The array can record the path , So how to output ? Please have a look at FindPath function
void FindPath(int i, int j, string& text1, string& text2) {
if (i < 0 || j < 0)return;
if (flag[i][j] == 1) {
FindPath(i - 1, j - 1, text1, text2);
cout << text1[i];//text2[j]
}
else if (flag[i][j] == 2)
{
FindPath(i, j - 1, text1, text2);
}
else
{
FindPath(i - 1, j, text1, text2);
}
}
The main function says :
int longestCommonSubsequence(string text1, string text2) {
dp.resize(text1.size(), vector<int>(text2.size())); flag.resize(text1.size(), vector<int>(text2.size()));
string path = "";
int ana = process(text1, text2, text1.size() - 1, text2.size() - 1);
FindPath(text1.size() - 1, text2.size() - 1, text1, text2);
return ana;
}
int main() {
cout << longestCommonSubsequence("YiQingTuiSan", "YIQINGTUISAN") << endl;
for (vector<int>t : flag) {
for (int i : t)cout << i << " ";
cout << endl;
}
return 0;
}

see ! That's the charm of recursion , We just need to define the conditions , Leave the rest to recursion, hahaha
Cough , Return to the right topic …
Now that we know how to write a memory search version , Then dynamic programming is easy to catch …
Dynamic programming
Note that there i And j from 1 Start , Because we put the boundary of the above memorized search into the loop , So add one more layer on both sides
void LCS()
{
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
if(s1[i-1] == s2[j-1]){
// notes : Here s1 And s2 The sequence is from s1[0] And s2[0] At the beginning
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;// Mark each situation , It is convenient to enter the path later
}
else{
//c[i][j] = max(c[i][j-1], c[i-1][j])
if(c[i][j-1] >= c[i-1][j]){
c[i][j] = c[i][j-1];
b[i][j] = 2; // The second case
}
else{
c[i][j] = c[i-1][j];
b[i][j] =3; // The third case
}
}
}
}
}
So should our output function also adapt to local conditions ?
void FindPath(int i, int j, string& text1, string& text2) {
if (i == 0 || j == 0)return;// It's different here , Remember the length of the upper pass
if (flag[i][j] == 1) {
FindPath(i - 1, j - 1, text1, text2);
cout << text1[i];//text2[j]
}
else if (flag[i][j] == 2)
{
FindPath(i, j - 1, text1, text2);
}
else
{
FindPath(i - 1, j, text1, text2);
}
}
版权声明
本文为[Stick to your original heart and go to your dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230138051533.html
边栏推荐
- Gbase 8s query processing and optimization
- Let's talk about passive safety again. I'll teach you to understand the rating report of China Insurance Research Institute collision test
- Introduction to gbase8s SQL Engine framework
- 紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
- Prince saves Princess (DFS)
- Gbase 8s检查点简介
- Encrypted compressed backup bat script
- 清研环境深交所上市:年营收1.8亿 市值41亿
- mb_ substr()、mb_ Strpos() function (story)
- Blocking granularity of gbase 8s concurrency control
猜你喜欢

LSF的使用方法总结

Redis实现分布式锁

Technology cloud report: cloud computing has entered the "second half". Where is the way out for domestic cloud?

2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation

The most easy to understand service container and scope of dependency injection

2022第六季完美童模 IPA國民賽領跑元宇宙賽道

全排列(DFS和next_permutation解法)

W801/W800/W806唯一ID/CPUID/FLASHID

无关联进程间通信 —— 命名管道的创建和使用

Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
随机推荐
计蒜客:等边三角形(DFS)
Gbase 8s存儲結構簡介及空間管理
JSP基础知识总结
Garlic Junkai company (DFS full arrangement)
Gbase 8s group by function introduction
使用单元测试框架编写单元测试的好处?
Detonate the bomb (DFS)
Android sqliteopenhelper data table structure upgrade
科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
GBASE 8s 共享内存缓冲池介绍
学习方法与职业发展指南(2022年版)
8GBs communication between client and server
Introduction to gbase 8s storage structure and space management
Garlic customer: equilateral triangle (DFS)
New functions of ai2022, introduction to new functions of illustrator 2022
JSP basic knowledge summary
最新流程引擎 flowable 6.7.2 更新说明
彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
计蒜客家谱(dfs求直系后代数)
42、使用mmrotate中k3det进行旋转目标检测,并进行mnn部署和ncnn部署