poj 1080 LCS 应用
来源:互联网 发布:微信交友源码 编辑:程序博客网 时间:2024/05/21 14:52
题意:给定两个DNA序列,然后按照给出的表格进行匹配,求匹配的最大值,利用lcs 原理,不过加了权值,就不能再像之前那样进行匹配了,但是原理是一样的。
因为有‘-’的存在,所以这边要先对边界进行处理一下。因为带权值,所以求得的不一定会是最长公共子序列,所以就不用判断str1[i]==str2[j]这种条件了。
转移方程:
dp[i][j]表示的是str1从0~i-1和str2从0~j-1 匹配的最大值。
那么dp[i][j]应该为dp[i-1][j-1]+(str1[i-1]和str2[j-1]的匹配值),dp[i-1][j]+(str1[i-1]和‘-’的匹配值),dp[i][j-1]+(‘-‘和str2[j-1]的匹配值) ,这三项中最大的一项。
很好解释,原理同lcs ,匹配得上,就(i-1,j-1 ) 匹配不上,要么(i-1,j)要么(i,j-1),只是加了权值要计算而已。
处理边界:
dp[0][0]=0;
然后枚举两个字符串的长度,就可以了
dp[i][0] = dp[i-1][0] + (str1[i-1]和’-‘匹配的值)
dp[0][i] = dp[0][i-1] + (’-‘和str2[i-1]匹配的值)
#include <stdio.h>#include <string.h>#include <algorithm>#pragma warning (disable : 4996)using namespace std;const int Max = 105;char str1[Max], str2[Max];int dp[Max][Max];int T, m, n;int map[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},};int Match(char c){if (c == 'A')return 0;if (c == 'C')return 1;if (c == 'G')return 2;if (c == 'T')return 3;if (c == '-')return 4;}void LCS(){dp[0][0] = 0;for (int i = 1; i <= m; i++)dp[i][0] = map[Match(str1[i - 1])][Match('-')] + dp[i - 1][0];for (int i = 1; i <= n; i++)dp[0][i] = map[Match('-')][Match(str2[i - 1])] + dp[0][i - 1];for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){dp[i][j] = max(dp[i - 1][j - 1] + map[Match(str1[i - 1])][Match(str2[j - 1])], dp[i][j]);dp[i][j] = max(dp[i - 1][j] + map[Match(str1[i - 1])][Match('-')], dp[i][j]);dp[i][j] = max(dp[i][j - 1] + map[Match('-')][Match(str2[j - 1])], dp[i][j]);}}}int main(){scanf("%d", &T);while (T--){scanf("%d %s", &m, str1);scanf("%d %s", &n, str2);LCS();printf("%d\n", dp[m][n]);}return 0;}
0 0
- poj 1080 LCS 应用
- POJ 1080 LCS扩展!
- POJ 1080 动态规划(LCS变种)
- POJ 1080 Human Gene Functions DP(LCS)
- POJ 1080 Human Gene Functions(LCS)
- POJ 1080 Human Gene Functions(LCS变形)
- POJ 1080 Human Gene Functions(DP LCS)
- POJ 1080 LCS变式dp
- poj 1080 Human Gene Functions lcs
- POJ 1080 Human Gene Functions(DP:LCS)
- POJ 1080 Human Gene Functions(LCS变形)
- poj 1159 Palindrome LCS
- poj 1159 Palindrome LCS
- poj 2533 LCS
- poj 3302 LCS水题
- poj 1159 LCS
- POJ 1458 LCS模板
- poj 1159 Palindrome (LCS)
- 黑马程序员----------------java基础-----------------Map集合
- 多校第七场 1011 hdu 5379 Mahjong tree(树形dp)
- hdu1075(trie树)
- 通用权限拦
- 六大原则
- poj 1080 LCS 应用
- jQuery_统计图插件-visifire
- 图像视觉各个领域文献目录
- 使用MFC操作EXCEL文件
- 左滑关闭demo
- 线段树模板
- 【Oracle】PL/SQL提取一段字符串中的每个字符
- 使用TextView可以实现跑马灯效果
- 策略模式