【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)
生活随笔
收集整理的這篇文章主要介紹了
【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:
給你兩個DNA序列(長度不一定相同),你可以在其中任意位置上加入空格,使得最終他倆長度相同,最終相同長度的兩個DNA序列會有個相似度比較(每個字符相對應的比較),問你如何放置這些空格使得總相似度最大。
題目告訴你了空格和空格比較的權值是0,這樣就保證了答案的有窮性。不然如果是正數的話就可以無窮大了(本也不符合常識)
解題報告:
? 類似編輯距離xjb搞就行了。dp[i][j]代表第一個序列前i個和第二個序列前j個完全匹配的最優解。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 400 + 5; const int INF = 0x3f3f3f3f; int sc[MAX][MAX]; int dp[MAX][MAX]; char s1[MAX],s2[MAX]; int Max(int a,int b,int c) {return max(a,max(b,c)); } int main() {sc['A']['A']=5;sc['C']['C']=5;sc['G']['G']=5;sc['T']['T']=5;sc['A']['C']=sc['C']['A']=-1;sc['A']['G']=sc['G']['A']=-2;sc['A']['T']=sc['T']['A']=-1;sc['C']['G']=sc['G']['C']=-3;sc['A'][' ']=sc[' ']['A']=-3;sc['C']['T']=sc['T']['C']=-2;sc['C'][' ']=sc[' ']['C']=-4;sc['G']['T']=sc['T']['G']=-2;sc['G'][' ']=sc[' ']['G']=-2;sc['T'][' ']=sc[' ']['T']=-1;int t;cin>>t;while(t--) {int len1,len2;scanf("%d%s%d%s",&len1,s1+1,&len2,s2+1);for(int i = 0; i<=len1; i++) for(int j = 0; j<=len2; j++) dp[i][j] = -INF; dp[0][0]=0;for(int i = 1; i<=len1; i++) dp[i][0] = dp[i-1][0] + sc[s1[i]][' '];for(int i = 1; i<=len2; i++) dp[0][i] = dp[0][i-1] + sc[s2[i]][' ']; for(int i = 1; i<=len1; i++) {for(int j = 1; j<=len2; j++) {dp[i][j] = Max(dp[i-1][j-1]+sc[s1[i]][s2[j]],dp[i-1][j]+sc[s1[i]][' '],dp[i][j-1]+sc[s2[j]][' ']);}} printf("%d\n",dp[len1][len2]);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QQ疑似出现大规模盗号!腾讯回应:用户扫
- 下一篇: 银行理财的净值化是不是风险变大了?净值型