(字符串的处理方式4.7.15)POJ 1080 Human Gene Functions(两个字符串的最佳匹配)

/*
 * POJ_1080.cpp
 *
 *  Created on: 2013年10月28日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int inf = -5;  //无穷小
int score['T' + 1]['T' + 1];  //积分表
const int maxn = 110;

void initial()  //打表
{
	score['A']['A'] = 5;//这种写法也是可以的
	score['C']['C'] = 5;
	score['G']['G'] = 5;
	score['T']['T'] = 5;
	score['-']['-'] = inf;
	score['A']['C'] = score['C']['A'] = -1;
	score['A']['G'] = score['G']['A'] = -2;
	score['A']['T'] = score['T']['A'] = -1;
	score['A']['-'] = score['-']['A'] = -3;
	score['C']['G'] = score['G']['C'] = -3;
	score['C']['T'] = score['T']['C'] = -2;
	score['C']['-'] = score['-']['C'] = -4;
	score['G']['T'] = score['T']['G'] = -2;
	score['G']['-'] = score['-']['G'] = -2;
	score['T']['-'] = score['-']['T'] = -1;
	return;
}

int main() {
	initial();

	int t;
	scanf("%d", &t);
	while (t--) {
		int len1, len2;
		char str1[maxn], str2[maxn];
		int dp[maxn][maxn];
		memset(dp, 0, sizeof(dp));

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

		//初始化
		dp[0][0] = 0;
		int i, j;
		for (i = 1; i <= len1; ++i) {
			dp[i][0] = dp[i-1][0] + score[str1[i-1]]['-'];
		}
		for(j = 1 ; j <= len2 ; ++j){
			dp[0][j] = dp[0][j-1] + score['-'][str2[j-1]];
		}

		//DP
		for (i = 1; i <= len1; ++i) {
			for (j = 1; j <= len2; ++j) {
				int temp1 = dp[i-1][j] + score[str1[i-1]]['-'];//str1[i-1]和空格匹配的最大得分
				int temp2 = dp[i][j-1] + score['-'][str2[j-1]];
				int temp3 = dp[i-1][j-1] + score[str1[i-1]][str2[j-1]];//str1[i-1]和str2[j-1]匹配的最大得分

				dp[i][j] = max(max(temp1,temp2),max(temp2,temp3));
			}
		}

		printf("%d\n",dp[len1][len2]);
	}

	return 0;
}

你可能感兴趣的:((字符串的处理方式4.7.15)POJ 1080 Human Gene Functions(两个字符串的最佳匹配))