本文主要是介绍POJ 1080 解题报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这道题看着就是DP题。基本上也就是DP题的模式。值得注意的是DP table的初始化,要把边界情况考虑清楚。比如DP[i][0]这种情况,处理的是从0到i都对应到0的情况,不应该疏忽地理解为只有i对应0。
thestoryofsnow | 1080 | Accepted | 180K | 0MS | C++ | 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;
}
这篇关于POJ 1080 解题报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!