nyoj 246 Human Gene Functions

题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=246

题意:给你两个字符串,字符串由“AGTC”四个字母组成,另外有“AGTC-”五个字符间两两结合的值。让两个字符串结合,空的地方补上‘-’。使得连个字符串结合后的对应的值最大。求最大值

解题思路:此题跟求最长公共子序类似,令x1,y1为ch1,ch2的子串,x1,y2的最大和为三种情况:

x1和y1比较,m1

x1和'-'比较,m2

y1和'-'比较,m3

dp[x1][y1]=max(m1,max(m2,m3));

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int fun(char s)
{
	switch(s)
	{
	case 'A':return 0;
	case 'C':return 1;
	case 'G':return 2;
	case 'T':return 3;
	case '-':return 4;
	}
}
int fs(char s1,char s2)
{
	int ch[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};
	return ch[fun(s1)][fun(s2)];
}
int main()
{
	int n;cin>>n;
	while(n--)
	{
		int m1,m2;char ch1[101],ch2[101];
		cin>>m1>>ch1;cin>>m2>>ch2;int dp[101][101];dp[0][0]=0;
		//令x1,y1为ch1,ch2的子串,x1,y2的最大和为三种情况
		//x1和y1比较,m1
		//x1和'-'比较,m2
		//y1和'-'比较,m3
		//dp[x1][y1]=max(m1,max(m2,m3));
		for(int i=0;i<m1;i++)
		{
			dp[i+1][0]=dp[i][0]+fs(ch1[i],'-');
		}
		for(int i=0;i<m2;i++)
		{
			dp[0][i+1]=dp[0][i]+fs(ch2[i],'-');
		}
		for(int i=0;i<m1;i++)
		{
			for(int j=0;j<m2;j++)
			{
				dp[i+1][j+1]=dp[i][j]+fs(ch1[i],ch2[j]);
				if(dp[i+1][j+1]<dp[i+1][j]+fs(ch2[j],'-'))dp[i+1][j+1]=dp[i+1][j]+fs('-',ch2[j]);
				if(dp[i+1][j+1]<dp[i][j+1]+fs(ch1[i],'-'))dp[i+1][j+1]=dp[i][j+1]+fs(ch1[i],'-');
			}
		}
		cout<<dp[m1][m2]<<endl;
	}
}


 

你可能感兴趣的:(nyoj 246 Human Gene Functions)