LCS 經典題及變式的小結 動態規劃

2021-06-18 11:03:16 字數 1248 閱讀 2594

lcs 是最大公共子串的意思。首先來一道最簡單的lcs題,即求兩個字串的lcs長度

//toj1683 lcs 最長公共子串行

#include #include using namespace std;

char s1[500],s2[500];

int res[500][500];

int main()

void lcs(char *s1,char *s2,int i,int j)

else if(num[i-1][j]>=num[i][j-1])

lcs(s1,s2,i-1,j);

else lcs(s1,s2,i,j-1);

}int main()

else if(num[i-1][j]>=num[i][j-1])

lcs(s1,s2,i-1,j);

else lcs(s1,s2,i,j-1);

}int main()

cin>>d;

j=0;

while(strcmp(d,"#")!=0)

len1=i;len2=j;

//cout<

poj1080 human gene functions

題目大意:

對給定的兩個字串(其中只包含acgt四個字母),以向需要的地方新增空格的方式使兩個字元等長,判定兩個字串的匹配最大程度。其匹配方式如題:

解題思路:

用乙個陣列f[110][110],f[i][j]表示母串錢 i 個字母與字串前 j 個字母的匹配值,此外我們用num[ i ][ j ]表示他們的匹配程度,於是不難得出狀態方程:

f[i][j]=max( f[i-1][j-1]+compute(s1[i-1],s2[j-1]),    f[i][j-1]+compute('-',s2[j-1]),    f[i-1][j]+compute(s1[i-1],'-')    )。

**如下

//toj1471 lcs 變式

#include#includeusing namespace std;

int a,b,f[105][105],i,j;

char s1[105],s2[105];

int num[6][6]=,,,

,,

};int get(char x)

}int compute(char x,char y)

int threemax(int a,int b,int c)

int main()

cout<

演算法 LCS及輸出LCS

最長公共子串行 include using namespace std typedef long long ll char a 1005 b 1005 pos i j 的三個值1,2,3分別表示左 左上和上 pos記錄當前lcs是從哪個點過來的,如果不等於2說明當前值是繼承自上乙個相等的點 所以如果...

O nlogn LIS及LCS演算法

morestep學長出題,考驗我們,第二題裸題但是資料範圍令人無奈,考試失利之後,刻意去學習了下優化的演算法 一 o nlogn 的lis 最長上公升子串行 設當前已經求出的最長上公升子串行長度為len。先判斷a t 與d len 若a t d len 則將a t 接在d len 後將得到乙個更長的...

O nlogn LIS及LCS演算法

morestep學長出題,考驗我們,第二題裸題但是資料範圍令人無奈,考試失利之後,刻意去學習了下優化的演算法 一 o nlogn 的lis 最長上公升子串行 設當前已經求出的最長上公升子串行長度為len。先判斷a t 與d len 若a t d len 則將a t 接在d len 後將得到乙個更長的...