题意:
LCS:
设dp[i][j]为前i,j的最长公共序列长度;
dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j])
dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
边界:dp[0][j] = 0(j<b.size) ,dp[i][0] = 0(i< a.size);
LCS变形:
设dp[i][j]为前i,j的最大价值:
value(x, y)为比较价值;
dp[i][j] = max(dp[i-1][j-1] + value(a[i],b[i]),dp[i][j-1] + value(‘-‘, b[i]), dp[i-1][j] + value(a[i],‘-‘);
边界:dp[i][0] = dp[i-1][0] + value(a[i],‘-‘),(i<=a.size); dp[0][j] = dp[0][j-1] + value(‘-‘,b[i]), (j <= b.size);
边界是个大问题!!
a[i]跟dp[i]的关系搞错.跪了整整一个小时啊!!!
#include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <queue> #include <stack> #include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define zero_(x,y) memset(x , y , sizeof(x)) #define zero(x) memset(x , 0 , sizeof(x)) #define MAX(x) memset(x , 0x3f ,sizeof(x)) #define swa(x,y) {LL s;s=x;x=y;y=s;} using namespace std ; #define N 105 const double PI = acos(-1.0); typedef long long LL ; char a[N],b[N]; int dp[N][N]; int value(char x, char y){ if(x == y)return 5; else if((x == ‘A‘&&y == ‘C‘)||(x == ‘C‘&&y == ‘A‘))return -1; else if((x == ‘A‘&&y == ‘G‘)||(x == ‘G‘&&y == ‘A‘))return -2; else if((x == ‘A‘&&y == ‘T‘)||(x == ‘T‘&&y == ‘A‘))return -1; else if((x == ‘C‘&&y == ‘G‘)||(x == ‘G‘&&y == ‘C‘))return -3; else if((x == ‘C‘&&y == ‘T‘)||(x == ‘T‘&&y == ‘C‘))return -2; else if((x == ‘T‘&&y == ‘G‘)||(x == ‘G‘&&y == ‘T‘))return -2; else if((x == ‘A‘&&y == ‘-‘)||(x == ‘-‘&&y == ‘A‘))return -3; else if((x == ‘C‘&&y == ‘-‘)||(x == ‘-‘&&y == ‘C‘))return -4; else if((x == ‘G‘&&y == ‘-‘)||(x == ‘-‘&&y == ‘G‘))return -2; else if((x == ‘T‘&&y == ‘-‘)||(x == ‘-‘&&y == ‘T‘))return -1; else return 0; } int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,aL,bL; cin>>n; while(n--){ cin>>aL; scanf("%s",a); cin>>bL; scanf("%s",b); dp[0][0] = 0; for(int i = 0;i < aL; i++) {dp[i+1][0] = dp[i][0] + value(a[i],‘-‘);} for(int i = 0;i < bL; i++) {dp[0][i+1] = dp[0][i] + value(b[i],‘-‘);} for(int i = 1;i <= aL; i++){ for(int j = 1;j <= bL; j++){ if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1] + value(a[i-1], b[j-1]); else dp[i][j] = max(dp[i-1][j-1]+value(a[i-1],b[j-1]), max(dp[i][j-1]+value(‘-‘,b[j-1]), dp[i-1][j]+value(a[i-1],‘-‘))); } } cout<<dp[aL][bL]<<endl; } return 0; }
时间: 2024-10-26 22:50:15