POJ 1080 解题报告

这道题看着就是DP题。基本上也就是DP题的模式。值得注意的是DP table的初始化,要把边界情况考虑清楚。比如DP[i][0]这种情况,处理的是从0到i都对应到0的情况,不应该疏忽地理解为只有i对应0。

thestoryofsnow1080Accepted180K0MSC++2210B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj1080 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXLEN = 100;


void ACGTto0123(char str[], int len)
{
	for (int i = 0; i < len; ++i)
	{
		if (str[i] == 'A')
		{
			str[i] = '0';
		}
		else if (str[i] == 'C')
		{
			str[i] = '1';
		}
		else if (str[i] == 'G')
		{
			str[i] = '2';
		}
		else
		{
			assert (str[i] == 'T');
			str[i] = '3';
		}
	}
}


int DP(char str1[], int len1, char str2[], int len2, const int scoreTable[][5], int scores[][MAXLEN + 1])
{
	scores[0][0] = 0;
	for (int i = 1; i <= len1; ++i)
	{
		int num1 = str1[i - 1] - '0';
		scores[i][0] = scores[i - 1][0] + scoreTable[num1][4];
	}
	for (int j = 1; j <= len2; ++j)
	{
		int num2 = str2[j - 1] - '0';
		scores[0][j] = scores[0][j - 1] + scoreTable[4][num2];
	}

	for (int i = 1; i <= len1; ++i)
	{
		for (int j = 1; j <= len2; ++j)
		{
			int num1 = str1[i - 1] - '0', num2 = str2[j - 1] - '0';
			// max of (str1[i] <-> str2[j], str1[i] <-> -)
			scores[i][j] = max(scores[i - 1][j - 1] + scoreTable[num1][num2], scores[i - 1][j] + scoreTable[num1][4]);
			// or (- <-> str2[j])
			scores[i][j] = max(scores[i][j], scores[i][j - 1] + scoreTable[4][num2]);
		}
	}

	return scores[len1][len2];
}

int main()
{
	const int scoreTable[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 T;	
	scanf("%d", &T);

	int len1, len2;
	char str1[MAXLEN + 1], str2[MAXLEN + 1];
	int scores[MAXLEN + 1][MAXLEN + 1];

	for (int t = 0; t < T; ++t)
	{
		scanf("%d", &len1);
		scanf("%s", str1);

		scanf("%d", &len2);
		scanf("%s", str2);

		// printf("[%s][%s]\n", str1, str2);

		ACGTto0123(str1, len1);
		ACGTto0123(str2, len2);

		// printf("[%s][%s]\n", str1, str2);		

		printf("%d\n", DP(str1, len1, str2, len2, scoreTable, scores));
	}
	return 0;  
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值