题意:
给你两个序列,序列的取值是(A,G,T,C),并且给出一个他们之间的对应价值矩阵
现在问题是,给你两个序列,你可以通过增加在字符串中间增加空格,最后要你求增加空格后的最大价值。
思路:可以增加空格,那么一个简单的思想是通过增加空格使得字母匹配数最大化,这样就可以获得最大价值。那么难免会出现不匹配的情况,我们也需要考虑。这题和LCS的思路基本上是一样的,唯一不同的是LCS的三种状态转移是有条件的,而这个题是没有条件的,即价值越大越好。举个例子
给出两个序列加空格的方式只有三种,那么我们可以从这个角度出现,逐步往长度大的求就行了。
定义dp[i] [j]:字符串s1从[1,i ]与字符串s2从[1,j ]的最大价值
转移方程:
字符价值矩阵我用map装了。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
map<char,map<char,int> >mp;
void lcs(int n,char s1[],int m,char s2[]){
for(int i = 1; i <= n; i++){
dp[0][i] = dp[0][i - 1] + mp['_'][s1[i]];
}
for(int i = 1; i <= m; i++){
dp[i][0] = dp[i - 1][0] + mp[s2[i]]['_'];
}
for(int i = 1; i <= m; i++ ){
for(int j = 1; j <= n; j++){
int l1 = dp[i][j - 1] + mp[s1[j]]['_'];
int l2 = dp[i - 1][j] + mp[s2[i]]['_'];
int l3 = dp[i - 1][j -1] + mp[s1[j]][s2[i]];
dp[i][j] = max(l1,max(l2,l3));
}
}
cout<<dp[m][n]<<endl;
}
int main(){
int t;
int g[][10]={{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}};
string s = "ACGT_";
for(int i = 0; i <s.size(); i++)
for(int j = 0; j <s.size(); j++)
mp[s[i]][s[j]] = g[i][j];
while(scanf("%d",&t)!=EOF){
while(t--){
int len1 ,len2;
scanf("%d%s",&len1,s1+1);
scanf("%d%s",&len2,s2+1);
lcs(len1,s1,len2,s2);
}
}
}
构造结果串
#include<bits/stdc++.h>
using namespace std;
/*
2
7 AGTGATG
5 GTTAG
3 AGT
2 GT
*/
const int maxn = 1e4 + 10;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int dir[maxn][maxn];
//dp[i][j]:串1前i个字符与串2前j的字符匹配的最大价值和
map<char,map<char,int> >mp;
int n,m;
string ans1,ans2;
void construct(int x,int y){
if(x < 0 || y < 0 || x > m || y > n)return;
if(dir[x][y] == 3){
construct(x-1,y-1);
ans1 += s2[x];
ans2 += s1[y];
}else if(dir[x][y] == 2){
construct(x - 1,y);
ans1 += s2[x];
ans2 += ' ';
}else if(dir[x][y] == 1){
construct(x,y - 1);
ans1 += ' ';
ans2 += s1[y];
}
}
void lcs(int n,char s1[],int m,char s2[]){
ans1.erase();ans2.erase();
memset(dp,0,sizeof(dp));
for(int i = 1; i <= n; i++){
dp[0][i] = dp[0][i - 1] + mp['_'][s1[i]];
dir[0][i] = 1;
// cout<<dp[0][i]<<" ";
}
cout<<endl;
for(int i = 1; i <= m; i++){
dp[i][0] = dp[i - 1][0] + mp[s2[i]]['_'];
dir[i][0] = 1;
// cout<<dp[i][0]<<" ";
}
for(int i = 1; i <= m; i++ ){
for(int j = 1; j <= n; j++){
int l1 = dp[i][j - 1] + mp[s1[j]]['_'];
int l2 = dp[i - 1][j] + mp[s2[i]]['_'];
int l3 = dp[i - 1][j - 1] + mp[s1[j]][s2[i]];
if(l1 > l2 && l1 > l3){
dir[i][j] = 1;//s1对上空格
}
if(l2 > l1 && l2 > l3){
dir[i][j] = 2;//s2对上空格
}
if(l3 > l1 && l3 > l2){
dir[i][j] = 3;//s1对上s2
}
dp[i][j] = max(l1,max(l2,l3));
}
}
//for(int i = 1; i <= m; i++){
// for(int j = 1; j <= n; j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
//}
//cout<<endl;
//for(int i = 0; i <= m; i++){
// for(int j = 0; j <= n; j++){
// cout<<dir[i][j]<<" ";
// }
// cout<<endl;
//}
construct(m,n);
cout<<"构造结果串:"<<'\n';
cout<<ans1<<endl<<ans2<<endl;
cout<<"最优值:"<<dp[m][n]<<endl;
}
int main(){
int t;
int g[][10]={{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}};
string s = "ACGT_";
for(int i = 0; i <s.size(); i++)
for(int j = 0; j <s.size(); j++)
mp[s[i]][s[j]] = g[i][j];
while(scanf("%d",&t)!=EOF){
while(t--){
int len1 ,len2;
scanf("%d%s",&len1,s1+1);
scanf("%d%s",&len2,s2+1);
n = len1,m = len2;
lcs(len1,s1,len2,s2);
}
}
}