Http://poj.org/problem? Id = 1080
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 27
/* Zoj 1027 poj 1080 idea: three states, maximum value: S1 [I] and S2 [J: DP [I-1] [J-1] + cost [my [S1 [I] [my [S2 [J]; S1 [I] and: DP [I-1] [J] + cost [my [S1 [I] [my ['-']; S2 [J] And: DP [I] [J-1] + cost [my ['-'] [my [S2 [J]; Note the boundary: d [I] [0] = cost [my [S1 [I] [my ['-'] + dp [I-1] [0]; it can only be configured with '-'. Similarly: DP [0] [J] = cost [my ['-'] [my [S2 [J] + dp [0] [J-1]; */# include <iostream> # include <map> # include <cstring> # include <string> using namespace STD; int cost [5] [5] = {5, -1,-2,-1,-3}, {-,-3,-2,-4}, {-2,-,-2, -2}, {-1,-2,-100000005,-1}, {-3,-4,-2,-1 }}; int DP [1005] [1005]; string S1, S2; Map <char, int> my; int main (INT argc, char * argv []) {int T, I, n, J, M; my ['a'] = 0; my ['C'] = 1; my ['G'] = 2; my ['T'] = 3; my ['-'] = 4; CIN> T; while (t --) {CIN> N> S1> m> S2; S1 = '0' + S1; S2 = '0' + S2; DP [0] [0] = 0; for (I = 1; I <= N; I ++) DP [I] [0] = cost [my [S1 [I] [my ['-'] + dp [I-1] [0]; for (j = 1; j <= m; j ++) DP [0] [J] = cost [my ['-'] [my [S2 [J] + dp [0] [J-1]; for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {int T1, T2, T3; t1 = DP [I-1] [J-1] + cost [my [S1 [I] [my [S2 [J]; t2 = DP [I-1] [J] + cost [my [S1 [I] [my ['-']; t3 = DP [I] [J-1] + cost [my ['-'] [my [S2 [J]; T1 = max (T1, T2 ); t1 = max (T1, T3); DP [I] [J] = T1;} cout <DP [N] [m] <Endl ;}return 0 ;}