POJ1080 Human Gene Functions
来源:互联网 发布:小米3支持4g网络吗 编辑:程序博客网 时间:2024/04/29 03:55
题意:
给出两组基因序列,给定每个基因字母与基因字母之间,以及基因字母与空格之间的相似度,求两组基因序列的最大相似度。其中,空格和空格不能相对。
思路:
此题和最长公共子序列的求解方法类似,利用动态规划进行求解。
假设 res[i][j] 表示基因序列a的前i个基因与基因序列b的前j个基因的最大相似度,则可得递推关系式:
res[i][j] = max(res[i - 1][j - 1] + V[a[i - 1]][b[j - 1]], res[i][j - 1] + V[' '][b[j - 1]], res[i - 1][j] + V[a[i - 1]][' ']);
即a[i - 1]与b[j - 1]相对、空格与b[j - 1]相对、a[i - 1]与空格相对这三者的最大值。
本题要注意的地方在于res[][]数组的初始化,初始化要全面。
res[0][0] = 0; res[0][j] = res[0][j - 1] + V(' ', b[j - 1]); res[i][0] = res[i - 1][0] + V(a[i - 1], ' ');
代码如下:
#include <iostream>using namespace std;#define MAXNUM 110int max(int a,int b,int c){ //求三个数的最大值 int max = -10000; if(max < a)max = a; if(max < b)max = b; if(max < c)max = c; return max;}void GetInput(int &p,int &q,int a[],int b[],char A[],char B[]){ cin>>p; getchar(); gets(A); cin>>q; getchar(); gets(B); int m = p; int n = q; for(int i = 0; i < m; i++) { if(A[i] == 'A')a[i] = 0; else if(A[i] == 'C')a[i] = 1; else if(A[i] == 'G')a[i] = 2; else if(A[i] == 'T')a[i] = 3; else if(A[i] == ' ')a[i] = 4; } for(int j = 0; j < n; j++) { if(B[j] == 'A')b[j] = 0; else if(B[j] == 'C')b[j] = 1; else if(B[j] == 'G')b[j] = 2; else if(B[j] == 'T')b[j] = 3; else if(B[j] == ' ')b[j] = 4; }}void Oninit(int p,int q,char A[],char B[],int a[],int b[],int res[MAXNUM][MAXNUM],int V[5][5]){ int m = p; int n = q; res[0][0] = 0; for(int i = 1; i <= m; i++) { res[i][0] = res[i - 1][0] + V[a[i - 1]][4]; } for(int j = 1; j <= n; j++) { res[0][j] = res[0][j - 1] + V[4][b[j - 1]]; }}void LCS(int p,int q,int a[],int b[],int res[MAXNUM][MAXNUM],int V[5][5]){ int m = p; int n = q; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { res[i][j] = max(res[i - 1][j - 1] + V[a[i - 1]][b[j - 1]],res[i][j - 1] + V[4][b[j - 1]],res[i - 1][j] + V[a[i - 1]][4]); } }}int main(){ int n = 0; //输入n组数据 int p = 0,q = 0; char A[MAXNUM]; char B[MAXNUM]; int a[MAXNUM] = {0}; int b[MAXNUM] = {0}; int res[MAXNUM][MAXNUM] = {0}; int V[5][5] = {{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}}; cin>>n; for(int i = 0; i < n; i++) { GetInput(p,q,a,b,A,B); //得到用户输入 Oninit(p,q,A,B,a,b,res,V); //初始化 LCS(p,q,a,b,res,V); cout<<res[p][q]<<endl; }}
1 0
- Human Gene Functions(POJ1080)
- poj1080 - Human Gene Functions
- POJ1080-Human Gene Functions
- POJ1080--Human Gene Functions
- poj1080 Human Gene Functions
- POJ1080 Human Gene Functions
- poj1080 Human Gene Functions
- 【POJ1080】【Human Gene Functions】
- POJ1080 Human Gene Functions
- poj1080 Human Gene Functions
- 【poj1080】 Human Gene Functions
- poj1080 Human Gene Functions
- POJ1080 Human Gene Functions DP
- Human Gene Functions poj1080 dp
- POJ1080 Human Gene Functions(带权LCS)
- poj1080 Human Gene Functions lcs 变形
- 北大ACM poj1080 Human Gene Functions
- POJ1080——Human Gene Functions
- Python实现动态规划切割钢条问题
- 细说KVO & KVC & NSNotificationCenter那些事
- 每次输出日志前需要判断日志的级别吗?
- Android的classloader
- kinetis时钟模块MCG详解
- POJ1080 Human Gene Functions
- android Matrix图片手势放大缩小,拖动
- Python开发环境搭建-Python(x,y) + Wing IDE4.0.3以及破解
- AT指令解惑
- Android之ViewTreeObserver
- 虚拟手机的创建步骤
- JAVA大数类详细介绍与题目应用举例
- 好用的圆圈 “加载加载” 效果 (也有dialog里面的加载)
- swift的一些基本属性8 (字典)