http://poj.org/problem?id=1080
知识点 :最长公共子序列
要点:
转移方程 f[i][j] = max{ f[i-i][j]+score[s1[i-1]]['-'], f[i][j-1]+score['-'][s2[j-1]], f[i-1][j-1]+score[s1[i-1]][s2[j-1]]}
#include <iostream> using namespace std; int score['T'+1]['T'+1]; int dp[1000][1000]; char s1[200],s2[200]; void init(){ score['A']['A']=5; score['C']['C'] =5; score['G']['G'] =5; score['T']['T'] =5; score['-']['-'] = -10; score['A']['C'] = score['C']['A']=-1; score['A']['G'] = score['G']['A']=-2; score['A']['T'] = score['T']['A']=-1; score['A']['-'] = score['-']['A']=-3; score['C']['G'] = score['G']['C']=-3; score['C']['T'] = score['T']['C']=-2; score['C']['-'] = score['-']['C']=-4; score['G']['T'] = score['T']['G']=-2; score['G']['-'] = score['-']['G']=-2; score['T']['-'] = score['-']['T']=-1; } int mx(int a,int b,int c){ int k = a>b?a:b; return c>k?c:k; } int main() { init(); int t; cin>>t; while(t--){ int len1,len2; cin>>len1>>s1>>len2>>s2; dp[0][0] =0; for(int i=1;i<=len1;i++){ dp[i][0] = dp[i-1][0]+score[s1[i-1]]['-']; } for(int j=1;j<=len2;j++){ dp[0][j] = dp[0][j-1]+score['-'][s2[j-1]]; } for(int i=1;i<=len1;i++){ for(int j=1;j<=len2;j++){ int temp1 = dp[i-1][j]+score[s1[i-1]]['-']; int temp2 = dp[i][j-1]+score['-'][s2[j-1]]; int temp3 = dp[i-1][j-1]+score[s1[i-1]][s2[j-1]]; dp[i][j] = mx(temp1,temp2,temp3); } } cout<<dp[len1][len2]<<endl; } return 0; }