您的位置:首页 > 其它

pku1080 Human Gene Functions DP

2016-03-16 08:33 465 查看
表示为串1匹配到第i位,串2匹配到第j位时的最大分数

复杂度N2,常数因为写法问题略微大了点

Problem: 1080 User: BPM136
Memory: 732K Time: 16MS
Language: G++ Result: Accepted

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 105
int f

;
char s1
,s2
;
int len1,len2;

int cost[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},
};
char exchange[5]={'A','C','G','T',' '};

int getvalue(char n,char m)
{
int t,tt;
fo(i,0,4)
{
if(exchange[i]==n)t=i;
if(exchange[i]==m)tt=i;
}
return cost[t][tt];
}

int DP()
{
memset(f,0,sizeof(f));
fo(i,1,len1)
{
f[i][0]=f[i-1][0]+getvalue(s1[i],' ');
}
fo(j,1,len2)
{
f[0][j]=f[0][j-1]+getvalue(' ',s2[j]);
}
fo(i,1,len1)
{
fo(j,1,len2)
{
if(s1[i]==s2[j])
{
f[i][j]=f[i-1][j-1]+getvalue(s1[i],s2[j]);
}else
{
int temp1=f[i-1][j]+getvalue(s1[i],' ');
int temp2=f[i][j-1]+getvalue(' ',s2[j]);
int temp3=f[i-1][j-1]+getvalue(s1[i],s2[j]);
f[i][j]=max(max(temp1,temp2),temp3);
}
}
}
return f[len1][len2];
}

int main()
{
int T=read();
while(T--)
{
scanf("%d%s",&len1,s1+1);
scanf("%d%s",&len2,s2+1);
printf("%d\n",DP());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp