题目点我
LCS变种
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define inf 0x3f3f3f3f
#define len 105
char a[len], b[len];
int dp[len][len], mat[len][len];
int Max(int a, int b){
return a > b ? a : b;
}
int main(){
int T, i, j;
mat['A']['A'] = mat['C']['C'] = mat['G']['G'] = mat['T']['T'] = 5;
mat['A']['C'] = mat['C']['A'] = mat['A']['T'] = mat['T']['A'] = mat['-']['T'] = -1;
mat['A']['G'] = mat['G']['A'] = mat['C']['T'] = mat['T']['C'] = mat['G']['T'] = mat['T']['G'] = mat['-']['G'] = -2;
mat['-']['A'] = mat['C']['G'] = mat['G']['C'] = -3;
mat['-']['C'] = -4;
scanf("%d", &T);
int len1, len2;
while(T--){
scanf("%d%s%d%s", &len1, a, &len2, b);
dp[0][0] = 0;
for(i = 1; i <= len1; i++)
dp[i][0] = dp[i-1][0] + mat['-'][a[i-1]];
for(j = 1; j <= len2; j++)
dp[0][j] = dp[0][j-1] + mat['-'][b[j-1]];
for(i = 1; i <= len1; i++){
for(j = 1; j <= len2; j++){
dp[i][j] = Max(dp[i-1][j-1] + mat[a[i-1]][b[j-1]], Max(dp[i-1][j] + mat['-'][a[i-1]], dp[i][j-1] + mat['-'][b[j-1]]));
}
}
printf("%d\n", dp[len1][len2]);
}
return 0;
}