当前位置:网站首页>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
边栏推荐
- Instructions for Jerry's reset IO maintenance level [chapter]
- Unity combines itextsharp to generate PDF preparation DLL
- Garlic Junkai company (DFS full arrangement)
- npm——配置淘宝镜像
- Gbase 8s检查点简介
- d盘分给C盘后,数据库恢复挂起怎么办,求各位解答
- 科技云报道:云计算进入“下半场”,国产云的出路在哪儿?
- Gbase 8s group by function introduction
- Solve the problem when installing MySQL
- When should I write unit tests? What is TDD?
猜你喜欢

Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)

彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?

腾讯云接口进行人脸检测 和签名出错问题

Prince saves Princess (DFS)
Let's talk about passive safety again. I'll teach you to understand the rating report of China Insurance Research Institute collision test

Self taught programming, don't read theory books foolishly, programmer: it's all left over by others

The most understandable life cycle of dependency injection

Chris LATTNER, father of llvm: the golden age of compilers

Problem solving: dpkg DEB: error: package name has characters that are't lowercase alphanums or '- +‘

leetcode771. Gemstones and stones
随机推荐
Counting garlic customers: Sudoku (DFS)
蒜头君开公司(DFS全排列)
关于C4D动画如何导入Lumion
iTextSharp 基础结构
K zeros after leetcode factorial function
Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
VSCODE + PHP Debug + 名字空间指引
Blocking granularity of gbase 8s concurrency control
Rôles attributs personnels Abréviations
Introduction to granularity locking of gbase 8s concurrency control
稳定币是让公链加速死亡或者取代BTC的超级机会?
Gbase 8t index
Gbase 8s数据库日志简介及管理
npm——配置淘宝镜像
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
Leetcode-阶乘函数后 K 个零
领导/老师让填写电子excel表格文档可手机上如何编辑word/excel文件填写excel/word电子文档?
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
W801/W800-wifi-socket开发(二)-UDP蓝牙控制wifi连接
GBASE 8s 共享内存缓冲池介绍