日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ1080 Human Gene Functions 动态规划 LCS的变形

發布時間:2023/12/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ1080 Human Gene Functions 动态规划 LCS的变形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意讀了半年,唉,給你兩串字符,然后長度不同,你能夠用‘-’把它們補成同樣長度,補在哪里取決于得分,它會給你一個得分表,問你最大得分


跟LCS非常像的DP數組 dp[i][j]表示第一個字符串取第i個元素第二個字符串取第三個元素,然后再預處理一個得分表加上就可以

得分表:

score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;score['A']['C'] = score['C']['A'] = -1;score['A']['G'] = score['G']['A'] = -2;score['A']['T'] = score['T']['A'] = -1;score['A']['-'] = score['-']['A'] = -3;score['C']['G'] = score['G']['C'] = -3;score['C']['T'] = score['T']['C'] = -2;score['C']['-'] = score['-']['C'] = -4;score['G']['T'] = score['T']['G'] = -2;score['G']['-'] = score['-']['G'] = -2;score['T']['-'] = score['-']['T'] = -1;score['-']['-'] = -inf;
那么DP方程就好推了:

dp[i][j] = :

dp[i-1][j] + score[s1[i-1]]['-']或者

dp[i][j-1] + score['-'][s2[j-1]]或者

dp[i-1][j-1] + score[s1[i-1]][s2[j-1]]或者

這三者之中取最大的

然后就是邊界問題我給忘記了

不夠細心,若單單是i==0或者j==0,邊界問題就出現了,邊界不可能為0的,所以還得處理一下邊界


#include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack> #include<map> #include<vector> #include<cmath> #include<memory.h> #include<set> #include<cctype>#define ll long long#define LL __int64#define eps 1e-8#define inf 0xfffffff//const LL INF = 1LL<<61;using namespace std;//vector<pair<int,int> > G; //typedef pair<int,int > P; //vector<pair<int,int> > ::iterator iter; // //map<ll,int >mp; //map<ll,int >::iterator p;const int N = 1000 + 5;int dp[N][N]; int score[200][200];void init() {score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;score['A']['C'] = score['C']['A'] = -1;score['A']['G'] = score['G']['A'] = -2;score['A']['T'] = score['T']['A'] = -1;score['A']['-'] = score['-']['A'] = -3;score['C']['G'] = score['G']['C'] = -3;score['C']['T'] = score['T']['C'] = -2;score['C']['-'] = score['-']['C'] = -4;score['G']['T'] = score['T']['G'] = -2;score['G']['-'] = score['-']['G'] = -2;score['T']['-'] = score['-']['T'] = -1;score['-']['-'] = -inf; }int main () {init();int t;char s1[N];char s2[N];scanf("%d",&t);while(t--) {int n,m;memset(dp,0,sizeof(dp));scanf("%d %s",&n,s1);scanf("%d %s",&m,s2);for(int i=1;i<=n;i++)dp[i][0] = dp[i-1][0] + score[s1[i-1]]['-'];//邊界處理for(int j=1;j<=m;j++)dp[0][j] = dp[0][j-1] + score['-'][s2[j-1]];//邊界處理for(int i=1;i<=n;i++) {for(int j=1;j<=m;j++) {int t1 = dp[i-1][j] + score[s1[i-1]]['-'];int t2 = dp[i][j-1] + score['-'][s2[j-1]];int t3 = dp[i-1][j-1] + score[s1[i-1]][s2[j-1]];int maxn = max(t1,t2);dp[i][j] = max(maxn,t3);}}printf("%d\n",dp[n][m]);}return 0; }

轉載于:https://www.cnblogs.com/mengfanrong/p/4087212.html

總結

以上是生活随笔為你收集整理的POJ1080 Human Gene Functions 动态规划 LCS的变形的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。