2015/2/13
说不出来是什么dp。。
问在给定矩阵条件的情况下两字符串的最优匹配程度。
那就类似最长公共子序列了...(虽然这样说没有一点科学依据但是你砍我啊!)
字符串s1 和字符串s2
for-> i s1
for->j s2
去遍历匹配,需要注意的是两者都可以匹配这个玩意 ‘ - ’
所以在匹配的时候需要判断下 (s1[ i ] - ' - ' 和 ' - ' - s2[ j ] 这样连接时不同的情况看过题目的就明白了)
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<climits>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define mod 10007
#define lson pos<<1,l,mid
#define sc(n) scanf("%d",&n)
#define rson pos<<1|1,mid+1,r
#define pr(n) printf("%d\n",n)
#define met(n,m) memset(n, m, sizeof(n))
#define F(x,y,i) for(int i = x;i > y; i--)
#define f(x,y,i) for(int i = x;i < y; i++)
#define ff(x,y,i) for(int i = x;i <= y; i++)
#define FF(x,y,i) for(int i = x;i >= y; i--)
const int N=100500;
const int inf = INT_MAX;
int Max(int a,int b)
{
return a>b?a:b;
}
int Min(int a,int b)
{
return a<b?a:b;
}
int len1,len2;
char s1[1005],s2[1005];
int tp[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
};
map<char,int> mp;
int dp[105][105];
int main()
{
int n, m, tot, x;
mp['A']= 0, mp['C']=1, mp['G']=2, mp['T']=3, mp['-'] = 4;
scanf("%d",&tot);
while(tot--)
{
scanf("%d%s",&len1,s1+1);
scanf("%d%s",&len2,s2+1);
dp[0][0] = 0;
f(1,len1+1,i)
dp[i][0] = tp[mp[s1[i]]][mp['-']] + dp[i-1][0];
f(1,len2+1,i)
dp[0][i] = tp[mp['-']][mp[s2[i]]]+dp[0][i-1];
f(1,len1+1,i)
f(1,len2+1,j)
{
dp[i][j] = dp[i-1][j-1] + tp[mp[s1[i]]][mp[s2[j]]];
dp[i][j] = Max(dp[i][j] , dp[i-1][j] + tp[mp[s1[i]]][mp['-']] );
dp[i][j] = Max(dp[i][j] , dp[i][j-1] + tp[mp['-']][mp[s2[j]]] );
}
printf("%d\n",dp[len1][len2]);
}
return 0;
}