poj——1080(dp)

题目地址:http://poj.org/problem?id=1080

解析:dp[i][j]来由:

1.dp[i][j-1]转移,则‘-’和s2[j]匹配。

2.dp[i-1][j]转移,则s1[i]和‘-’匹配。

3.dp[i-1][j-1],则s1[i]和s2[j]匹配。三者取最大值即可。

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
#define INF 0x7fffffff
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
#define N 103
int len2,len1;
int  dp[N][N];
char s1[N],s2[N];
int  match[N][N];
void init(){
	match['A']['T']=-1;match['T']['A']=-1;match['A']['A']=5;match['T']['T']=5;
	match['A']['C']=-1;match['C']['A']=-1;match['A']['G']=-2;match['G']['A']=-2;
	match['C']['G']=-3;match['G']['C']=-3;match['G']['G']=5;match['C']['C']=5;
	match['C']['T']=-2;match['T']['C']=-2;match['T']['G']=-2;match['G']['T']=-2;
	match['A']['-']=-3;match['-']['A']=-3;match['-']['C']=-4;match['C']['-']=-4;
	match['G']['-']=-2;match['-']['G']=-2;match['-']['T']=-1;match['T']['-']=-1;
}
void solve(){
	int i,j;
	dp[0][0]=0;
	for(i=1;i<=len1;i++)
	  dp[i][0]=dp[i-1][0]+match[s1[i]]['-'];
    for(i=1;i<=len2;i++)
      dp[0][i]=dp[0][i-1]+match['-'][s2[i]];
    for(i=1;i<=len1;i++)
     for(j=1;j<=len2;j++) 
     {
     	dp[i][j]=max(dp[i-1][j-1]+match[s1[i]][s2[j]],max(dp[i-1][j]+match[s1[i]]['-'],dp[i][j-1]+match['-'][s2[j]]));
     }
     cout<<dp[len1][len2]<<endl;
}
int main()
{
	  int t;
	  cin>>t;
	  init();
      while(t--){
      	cin>>len1>>s1+1;
      	cin>>len2>>s2+1;
      	solve();
      }
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值