【求助】
查看原帖
【求助】
391427
煌凉一梦楼主2024/4/2 21:05
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;
// 基因表
int geneBeta[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 genetonum(char x)
{
	if (x == 'A')
		return 0;
	if (x == 'C')
		return 1;
	if (x == 'G')
		return 2;
	if (x == 'T')
		return 3;
	if (x == '-')
		return 4;
}
int main()
{
	int length_1;
	string gene_1;
	int length_2;
	string gene_2;
	cin >> length_1 >> gene_1;
	cin >> length_2 >> gene_2;
	string gene_be; // 作为对照的基因
	int length_be;
	string gene_to; // 去比较的基因
	int length_to;
	if (length_1 >= length_2)
	{
		gene_be = gene_1;
		length_be = length_1;
		gene_to = gene_2;
		length_to = length_2;
	}
	else
	{
		gene_be = gene_2;
		length_be = length_2;
		gene_to = gene_1;
		length_to = length_1;
	}
	

	// dp[i][j] 对照到第i个基因时,第二个基因对使用到第j个基因,此时的相似度最大
	int dp[101][101];
	// 为了方便代码,选择从1开始
	dp[0][0] = 0;
	dp[0][1] = geneBeta[4][genetonum(gene_to[0])];
	for (int i = 1; i <= length_be; i++)
	{
		char ch_1 = gene_be[i - 1];
		for (int j = 0; j <= i && j <= length_to; j++)
		{
			// cout << i << " " << j << endl;
			// 如果j为0,只能是继续使用“-”匹配
			if (j == 0)
			{
				dp[i][j] = dp[i - 1][j] + geneBeta[genetonum(ch_1)][4];
				// cout<< geneBeta[genetonum(ch_1)][4] << endl;
				// cout << ch_1 << endl;
			}
			else
			{
				char ch_2 = gene_to[j - 1];
				// 一共有三种情况
				// ch_1对上-
				int temp_1 = dp[i - 1][j] + geneBeta[genetonum(ch_1)][4];
				// ch_1对上ch_2
				int temp_2 = dp[i - 1][j - 1] + geneBeta[genetonum(ch_1)][genetonum(ch_2)];
				// - 对上ch_2
				int temp_3 = dp[i][j - 1] + geneBeta[4][genetonum(ch_2)];
				dp[i][j] = max(temp_1, max(temp_2, temp_3));
 			} 
			// cout << i<<" "<<j<<" " << dp[i][j] << endl;
		}
	}
	cout << dp[length_be][length_to];
}
2024/4/2 21:05
加载中...