C 動態規劃 最長公共子串行和最長公共子串

2021-10-23 02:37:02 字數 1468 閱讀 9029

寫在前面:本文矩陣思路**於這篇博文,感謝,侵刪

flag矩陣思路**於這篇博文,感謝,侵刪

#include

#include

#include

#include

using

namespace std;

const

int maxn =

100;

char str1[maxn]

;char str2[maxn]

;void

lcs1

(int

** dp,

int*

* flag,

int n,

int m)

//最長公共子串行

else

if(dp[i-1]

[j]>= dp[i]

[j-1])

else

if(dp[i-1]

[j]< dp[i]

[j-1])

printf

("%d "

,dp[i]

[j]);}

printf

("\n");

}printf

("%d\n"

,dp[n]

[m]);}

void

lcs2

(int

** dp,

int*

* flag,

int n,

int m)

//最長公共子串}}

}printf

("%d\n"

,answer)

;char temp[maxn]=;

strncpy

(temp,str1+index-answer+

1,answer)

;//strncpy(想要的結果,起始位址(注意是位址,不是下標),長度)

printf

("%s\n"

,temp);}

void

printlcs

(int

** dp,

int*

* flag,

int i,

int j)

else

if(flag[i]

[j]==2)

printlcs

(dp,flag,i-

1,j)

;else

if(flag[i]

[j]==3)

printlcs

(dp,flag,i,j-1)

;}intmain()

lcs1

(dp,flag,n,m)

;printlcs

(dp,flag,n,m)

;free

(dp)

;free

(flag);}

return0;

}

動態規劃 最長公共子串行和最長公共子串

我們首先需要搞清楚以下兩個概念 最長公共子串行 vs 最長公共子串 找兩個字串的最長公共子串,這個子串要求在原字串中是連續的。而最長公共子串行則並不要求連續。問題描述 給定兩個字串,求解這兩個字串的最長公共子串行 longest common sequence 比如字串1 bdcaba 字串2 ab...

動態規劃 最長公共子串行

問題描述 我們稱序列z z1,z2,zk 是序列x x1,x2,xm 的子串行當且僅當存在嚴格上公升的序列 i1,i2,ik 使得對j 1,2,k,有xij zj。比如z a,b,f,c 是x a,b,c,f,b,c 的子串行。現在給出兩個序列x和y,你的任務是找到x和y的最大公共子串行,也就是說要...

動態規劃 最長公共子串行

兩個序列的最長公共子序 lcs longest common length 的 每個字元可以不連續,如x y 那麼它們的最長公共子串行為。這是乙個經典的動態規劃問題,著手點還是找到 最精髓的 狀態轉移方程 假設x,y兩個序列的前i,j個位置的最大子串行已經找到為r i j 自底往上 那麼x i 與y...