POJ 1080--Human Gene Function
2014-08-05 19:45
260 查看
本题DP方案类似于LCS的DP设计。
考虑到两个长度分别为L1和L2的串s1,s2,匹配后的两串末尾字符只有三种可能:
(s1[L1],—),(s1[L1],s2[L2]),(—,s2[L2])。
设dp[i][j]为s1[i]之前的字符串和s2[j]之前的字符串所能匹配到的最大得分。
由定义及上面的结论很容易得出:
若i = 0,dp[0][j] = sum{score[s2[k]][4],1 <= k <= j},其中score数组为相应字符得分。同理可求得dp[i][0]。
若s1[i] = s2[j],易知dp[i][j] = dp[i-1][j-1]+1。
若s1[i] != s2[j],易知dp[i][j] = max{dp[i-1][j]+score[s1[i]][4],dp[i][j-1]+score[s2[j]][4],dp[i-1][j-1]+score[s1[i]][s2[j]]}。
考虑到两个长度分别为L1和L2的串s1,s2,匹配后的两串末尾字符只有三种可能:
(s1[L1],—),(s1[L1],s2[L2]),(—,s2[L2])。
设dp[i][j]为s1[i]之前的字符串和s2[j]之前的字符串所能匹配到的最大得分。
由定义及上面的结论很容易得出:
若i = 0,dp[0][j] = sum{score[s2[k]][4],1 <= k <= j},其中score数组为相应字符得分。同理可求得dp[i][0]。
若s1[i] = s2[j],易知dp[i][j] = dp[i-1][j-1]+1。
若s1[i] != s2[j],易知dp[i][j] = max{dp[i-1][j]+score[s1[i]][4],dp[i][j-1]+score[s2[j]][4],dp[i-1][j-1]+score[s1[i]][s2[j]]}。
#include<cstdio> #include<cstring> #define maxL 102 short _max(short x,short y,short z) { short maxVal = x; if(maxVal < y) maxVal = y; if(maxVal < z) maxVal = z; return maxVal; } int encode(char c) { switch(c) { case 'A': return 0; case 'C': return 1; case 'G': return 2; case 'T': return 3; case '\n': return '\n'; default: return 0; } } int strIn(int* L,char* s) { scanf("%d",L); getchar(); for(int i = 0;i < *L;i++) *(s++) = encode(getchar()); return 0; } int main() { int i,j; int T; int L1,L2; char s1[maxL],s2[maxL]; short maxScore[maxL][maxL]; short score[4][5] = {{5,-1,-2,-1,-3}, {-1,5,-3,-2,-4}, {-2,-3,5,-2,-2}, {-1,-2,-2,5,-1}}; while(~scanf("%d",&T)) { while(T--) { memset(maxScore,0,sizeof(maxScore)); strIn(&L1,s1+1); strIn(&L2,s2+1); for(i = 1;i <= L1;i++) maxScore[i][0] = maxScore[i-1][0]+score[s1[i]][4]; for(i = 1;i <= L2;i++) maxScore[0][i] = maxScore[0][i-1]+score[s2[i]][4]; for(i = 1;i <= L1;i++) { for(j = 1;j <= L2;j++) { if(s1[i] == s2[j]) maxScore[i][j] = maxScore[i-1][j-1]+5; else maxScore[i][j] = _max(maxScore[i-1][j]+score[s1[i]][4],maxScore[i][j-1]+score[s2[j]][4],maxScore[i-1][j-1]+score[s1[i]][s2[j]]); } } printf("%d\n",maxScore[L1][L2]); } } return 0; }
相关文章推荐
- poj1080——human gene function
- 【算法刷题】poj 1080 Human Gene Function
- poj 1080 Human Gene Functions
- POJ 1080-Human Gene Functions(LCS变形-最大得分)
- 【POJ 1080】 Human Gene Functions
- POJ 1080 Human Gene Functions DP
- PKU ACM 1080-human gene function
- POJ 1080 Human Gene Functions
- poj 1080 -- Human Gene Functions
- POJ 1080 Human Gene Functions
- poj 1080 Human Gene Functions
- POJ 1080, Human Gene Functions
- POJ-1080-Human Gene Functions
- poj 1080 Human Gene Functions
- POJ-1080- Human Gene Functions-DP-LCS变形
- poj--1080 Human Gene Functions(dp)
- poj-1080 Human Gene Functions **
- POJ 1080 Human Gene Functions
- POJ 1080 Human Gene Functions[LCS]
- POJ 1080 Human Gene Functions