一个动态规划的题,今天看了一个下午,感觉看懂了,就写了下,可能不是我看懂了,只是我记性比较好吧!
#include<iostream> #include<string> using namespace std;
int gene[150][150]; //到第一个字符串i和第二个字符串j的位置时最大的值 int n; //测试组数 int len1,len2; //两个字符串的长度 string str1,str2; //带求的两个字符串 int score[5][5] = { //字符与字符及“—”的对应值 {5,-1,-2,-1,-3}, {-1,5,-3,-2,-4}, {-2,-3,5,-2,-2}, {-1,-2,-2,5,-1}, {-3,-4,-2,-1,0} };
char mp[130]; void solve() { int i,j; memset(gene,0,sizeof(gene));
gene[0][0] = 0; for(i = 1;i <= len2;i++) { gene[0][i] = gene[0][i-1] + score[4][mp[str2[i-1]]]; } for(i = 1;i <= len1;i++) { gene[i][0] = gene[i-1][0] + score[mp[str1[i-1]]][4]; } int m1,m2,m3,mmax; for(i = 1;i <= len1;i++) { for(j = 1; j <= len2;j++) { m1 = gene[i - 1][j - 1] + score[mp[str1[i-1]]][mp[str2[j-1]]]; m2 = gene[i][j - 1] + score[4][mp[str2[j-1]]]; m3 = gene[i - 1][j] + score[mp[str1[i-1]]][4];
mmax = m1; if(mmax < m2) mmax = m2; if(mmax < m3) mmax = m3; gene[i][j] = mmax; } } cout<<gene[len1][len2]<<endl; } int main() { mp['A'] = 0; mp['C'] = 1; mp['G'] = 2; mp['T'] = 3; mp['-'] = 4;
cin>>n; while(n--) { cin>>len1 >> str1; cin>>len2 >> str2; solve(); } return 0; }
poj1080解题报告
最新推荐文章于 2018-09-12 14:49:25 发布