Topic link:
https://cn.vjudge.net/problem/POJ-1080
Topic meaning:
Given two sets of sequences, you are asked to find their maximum similarity. Each letter has a score corresponding to other letters or itself and spaces. Insert spaces into these two strings, so that the two strings have a score. Maximum match score
Problem solving ideas:
Similar to LCS, use dp[i][j] to represent the optimal solution of the top i bits of s1 and the top j bits of s2.
The recursion is:
Forget the parentheses
dp[i][j] is only recursively derived from dp[i-1][j-1]
if(s1[i] == s2[j])dp[i][j] = dp[i - 1][j - 1] + 5
else dp[i][j] = dp[i - 1][j - 1] + Map[id1][id2] where Map[id1][id2] is the matching score of s1[i] and s2[j]
consider parentheses
dp[i][j] can be recursively derived from dp[i-1][j] and dp[i][j-1] on the original basis
When recursively derived from dp[i-1][j], s1[i] matches spaces
When recursively derived from dp[i][j-1], the space matches s2[j]
dp[i][j] = max(dp[i][j], dp[i-1][j] + Map[id1][space], dp[i][j - 1] + Map[space] [id2])
Notice:
To initialize the values of dp[0][i] and dp[j][0] (cannot simply set to 0, it is wrong to set 0), dp[0][0]=0
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #include<stack> 8 #include<map> 9 #include<sstream> 10 #define Mem(a, b) memset(a, b, sizeof(a)) 11 using namespace std; 12 typedef long long ll; 13 const int INF = 1e9+7; 14 int dp[105][105]; 15 int Map[5][5] = 16 { 17 5,-1,-2,-1,-3, 18 -1,5,-3,-2,-4, 19 -2,-3,5,-2,-2, 20 -1,-2,-2,5,-1, 21 -3,-4,-2,-1,0, 22 }; 23 char s1[105], s2[105]; 24 int main() 25 { 26 map<char, int>id; 27 id['A'] = 0; 28 id['C'] = 1; 29 id['G'] = 2; 30 id['T'] = 3; 31 id[' '] = 4; 32 int T, n, m; 33 cin >> T; 34 while(T--) 35 { 36 cin >> n >> (s1 + 1); 37 cin >> m >> (s2 + 1); 38 Mem(dp, 0); 39 for(int i = 1; i <= n; i++) 40 { 41 dp[i][0] = dp[i - 1][0] + Map[id[s1[i]]][id[' ']]; 42 } 43 for(int i = 1; i <= m; i++) 44 { 45 dp[0][i] = dp[0][i - 1] + Map[id[' ']][id[s2[i]]]; 46 } 47 for(int i = 1; i <= n; i++) 48 { 49 for(int j = 1; j <= m; j++) 50 { 51 if(s1[i] == s2[j]) 52 dp[i][j] = dp[i - 1][j - 1] + 5; 53 else 54 dp[i][j] = dp[i - 1][j - 1] + Map[id[s1[i]]][id[s2[j]]]; 55 //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; 56 dp[i][j] = max(dp[i][j],dp[i - 1][j] + Map[id[s1[i]]][id[' ']]); 57 dp[i][j] = max(dp[i][j], dp[i][j - 1] + Map[id[' ']][id[s2[j]]]); 58 //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; 59 } 60 // 61 } 62 cout<<dp[n][m]<<endl; 63 } 64 return 0; 65 }