标签:数值 course ble sum func lcs show ges enter
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19885 | Accepted: 11100 |
Description
Input
Output
Sample Input
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
Sample Output
14 21
题目大意 :
给出一个基因匹配表格 , 里面有一些数值 。
设 dp[i][j] 为 s1 取第 i 个字符, s2 取第 j 个字符的最大值,决定dp[i][j] 最优的情况有 三种, 类似于最长公共子序列的三种情况。
1 . s1 取第 i 个字母, s2 取 ‘-‘ , temp1 = dp[i][j] = dp[i-1][j] + score[a[i]][‘-‘];
2 . s2 取第 j 个字母, s1 取 ‘-‘ , temp2 = dp[i][j] = dp[i][j-1] + score[‘-‘][b[j]];
3 . s1 取第 i 个字母 , s2 取第 j 个字母 , temp3 = dp[i][j] = dp[i-1][j-1] + score[a[i]][b[j]];
则 dp[i][j] = max ( temp1, temp2, temp3 );
还有初始化问题 :
dp[0][0] = 0;
dp[i][0] = dp[i-1][0] + score[a[i]][‘-‘];
dp[0][j] = dp[0][j-1] + score[‘-‘][b[j]];
代码示例 :
/* * Author: ry * Created Time: 2017/9/3 8:00:06 * File Name: 1.cpp */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <time.h> using namespace std; const int mm = 1e6+5; #define ll long long ll t_cnt; void t_st(){t_cnt=clock();} void t_ot(){printf("you spent : %lldms\n", clock()-t_cnt);} //开始t_st(); //结束t_ot(); int dp[150][150]; int score[‘T‘+1][‘T‘+1]; void intial () { score[‘A‘][‘A‘] = 5; score[‘C‘][‘C‘] = 5; score[‘G‘][‘G‘] = 5; 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; } int MAX (int x, int y, int z) { int k = (x>y?x:y); return z>k?z:k; } int main() { int t; int len1, len2; char a[105], b[105]; intial(); cin >> t; getchar(); while ( t-- ){ memset (dp, 0, sizeof(dp)); scanf("%d %s", &len1, a); scanf("%d %s", &len2, b); for (int i = len1; i > 0; i--) a[i] = a[i-1]; for (int i = len2; i > 0; i--) b[i] = b[i-1]; dp[0][0] = 0; for (int i = 1; i <= len1; i++) dp[i][0] = dp[i-1][0] + score[a[i]][‘-‘]; for (int j = 1; j <= len2; j++) dp[0][j] = dp[0][j-1] + score[‘-‘][b[j]]; for (int i = 1; i <= len1; i++) for (int j = 1; j <= len2; j++){ int temp1 = dp[i-1][j] + score[a[i]][‘-‘]; int temp2 = dp[i][j-1] + score[‘-‘][b[j]]; int temp3 = dp[i-1][j-1] + score[a[i]][b[j]]; dp[i][j] = MAX (temp1, temp2, temp3); } printf ("%d\n", dp[len1][len2]); } return 0; }
标签:数值 course ble sum func lcs show ges enter
原文地址:http://www.cnblogs.com/ccut-ry/p/7469071.html