Problem Description
It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their functions, because
these can be used to diagnose human diseases and to design new drugs for them.
A human gene can be identified through a series of time-consuming biological experiments, often with the help of computer programs. Once a sequence of a gene is obtained, the next job is to determine its function. One of the methods for biologists to use in
determining the function of a new gene sequence that they have just identified is to search a database with the new gene as a query. The database to be searched stores many gene sequences and their functions – many researchers have been submitting their genes
and functions to the database and the database is freely accessible through the Internet.
A database search will return a list of gene sequences from the database that are similar to the query gene. Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might be one of the functions that
the genes from the list have. To exactly determine which one is the right one another series of biological experiments will be needed.
Your job is to make a program that compares two genes and determines their similarity as explained below. Your program may be used as a part of the database search if you can provide an efficient one.
Given two genes AGTGATG and GTTAG, how similar are they? One of the methods to measure the similarity of two genes is called alignment. In an alignment, spaces are inserted, if necessary, in appropriate positions of the genes to make them equally long and score
the resulting genes according to a scoring matrix.
For example, one space is inserted into AGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG to result in –GT--TAG. A space is denoted by a minus sign (-). The two genes are now of equal length. These two strings are aligned:
AGTGAT-G
-GT--TAG
In this alignment, there are four matches, namely, G in the second position, T in the third, T in the sixth, and G in the eighth. Each pair of aligned characters is assigned a score according to the following scoring matrix.
* denotes that a space-space match is not allowed. The score of the alignment above is (-3)+5+5+(-2)+(-3)+5+(-3)+5=9.
Of course, many other alignments are possible. One is shown below (a different number of spaces are inserted into different positions):
AGTGATG
-GTTA-G
This alignment gives a score of (-3)+5+5+(-2)+5+(-1) +5=14. So, this one is better than the previous one. As a matter of fact, this one is optimal since no other alignment can have a higher score. So, it is said that the similarity of the two genes is 14.
Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case consists of two lines: each line contains an integer, the length of a gene, followed by a gene sequence. The length of each gene sequence
is at least one and does not exceed 100.
Output
The output should print the similarity of each test case, one per line.
Sample Input
2
7 AGTGATG
5 GTTAG
7 AGCTATT
9 AGCTTTAAA
Sample Output
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
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,-100000}
};
int opt[110][110]; //opt[i][j] 表示 字符串1的前i个和字符串2的前j个最大匹配度
int i,j,t,m,n;
char str1[110],str2[110];
int arr1[110],arr2[110];
int index[256];
int mymax(int a,int b,int c){
int t = a > b ? a : b;
return t > c ? t : c;
}
int main() {
index['A'] = 0;
index['C'] = 1;
index['G'] = 2;
index['T'] = 3;
index['-'] = 4;
//freopen("input.txt","r", stdin);
cin >> t;
while(t--){
scanf("%d %s %d %s",&m,str1,&n,str2);
opt[0][0] = 0;
for( i=1; i<=m; i++){ //处理第一列
arr1[i] = index[str1[i-1]];
opt[i][0] =opt[i-1][0] + map[arr1[i]][4];
}
for( i=1; i<=n; i++){ //处理第一行
arr2[i] = index[str2[i-1]];
opt[0][i] =opt[0][i-1] + map[arr2[i]][4];
}
for(i=1; i<=m; i++){
for(j=1; j<=n; j++){
opt[i][j] = mymax( opt[i-1][j-1] + map[ arr1[i] ][ arr2[j] ],
opt[i-1][j] + map[arr1[i]][4], opt[i][j-1] + map[arr2[j]][4]);
}
}
cout << opt[m][n] << endl;
}
return 0;
}
分享到:
相关推荐
简单实用简
memlog_lcs-1-lcs-0-rcm_1.tar
前端开源库-metric-lcs度量LCS,零依赖度量JS中最长的公共子序列实现
#数据结构邓神Q:为什么算法正确:1: 单调性: 论如何,每经过次对,原来的问题规模必然减少具体的,作为输的两个序列,少其的度缩短个单位最好情况下(也就是只有
最长公共子串匹配动态规划算法实现,源代码加注释,可运行啊,这描述限定真死板,还20 字符,吃多了啊
GitDiffLCS 通常,git diff --stat不包含修改,因此我们添加了它以使用LCS算法显示修改。安装$ gem install git_diff-lcs如何使用$ git_diff_lcs shortstat [GIT_REPOSITORY or WORKING_DIRECTORY] [SRC(branch or ...
最长公共子序列,动态规划算法可有效地解此问题。下面我们按照动态规划算法设计的各个步骤来设计一个解此问题的有效算法
Javascript-lcs Javascript 中的字符串比较。 这个 Javascript 库使用最长字符串子序列算法来进行字符串比较。 用 git 克隆: git clone git://github.com/sp4ce/javascript-lcs cd javascript-lcs 代码示例: ...
licenta-lcs 自动从code.google.com/p/licenta-lcs导出 控制城市内公共交通的LCS系统。
借助LCS算法实现文本文档比对,可以自行扩展支持word等格式文件
rpm安装包,rpm -i example.rpm
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用
官方离线安装包,亲测可用
官方离线安装包,亲测可用
DAA-P05-LCS-动态编程
官方离线安装包,亲测可用
动态规划:最长公共子串 LCS 动态规划:最长公共子串 LCS
Haier海尔家用消毒柜ZQD100F-12LCS1使用说明书用户手册图解图示pdf电子版下载