###### KusanoNEU的博客

No day but today.

###### RNA secondary structure(Dynamic Programming)
//
//  main.cpp
//  RNA
//
//  Created by Longxiang Lyu on 6/1/16.
//

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool complement(char c1, char c2)
{
return  (c1 == 'A' && c2 == 'U') ||
(c1 == 'U' && c2 == 'A') ||
(c1 == 'C' && c2 == 'G') ||
(c1 == 'G' && c2 == 'C');
}

void findSolution(const vector<vector<int>> &M, int i, int j, vector<pair<int, int>> &pairs)
{
for (int t = i; t < j - 4; ++t)
{
if (M[i][j] == 1 + M[i][t - 1] + M[t + 1][j - 1])
{
pairs.push_back({t, j});
findSolution(M, i, t - 1, pairs);
findSolution(M, t + 1, j - 1, pairs);
}
}
}

int RNA(string seq, vector<pair<int, int>> &pairs)
{
for (auto &c : seq)
c = toupper(c);
int n = static_cast<int>(seq.length() - 1);
vector<vector<int>> M(n + 1, vector<int>(n + 1, 0));

for (int interval = 5; interval <= n -1; ++interval)
{
for (int i = 1; i <= n - interval; ++i)
{
// compute M[i][j]
int j = i + interval;
int opt = M[i][j - 1];      // case 1: bj is not involved in pair

for (int t = i; t < j - 4; ++t)
{
if (complement(seq[t], seq[j]) && opt < 1 + M[i][t - 1] + M[t + 1][j - 1])
{
opt = 1 + M[i][t - 1] + M[t + 1][j - 1];        // case 2: choose the largest pair sum
}
}
M[i][j] = opt;
}
}
findSolution(M, 1, n, pairs);
return M[1].back();
}

int main(int argc, const char * argv[])
{
string seq = "*AUGUGGGCAU";
vector<pair<int, int>> ret;
cout << "Max number of pairs: " << RNA(seq, ret) << endl;
for (auto a : ret)
{
cout << a.first << " - " << a.second << endl;
}

return 0;
}


Reference:

http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/06DynamicProgrammingI.pdf

#### RNA Secondary Structure Prediction7.rar

2011年12月25日 12.18MB 下载

#### RNAstructure

2010年04月21日 4.13MB 下载

#### 动态规划(dynamic programming)原理

2016-04-30 17:21:02

#### 动态规划（dynamic programming）初步入门

2016-03-22 10:16:39

#### 五大常用算法 ----DP 动态规划（Dynamic Programming）

2016-07-24 22:35:21

#### 【动态规划】Dynamic Programming

2014-11-27 16:41:49

#### 动态规划(Dynamic Programming)与贪心算法(Greedy Algorithms)

2009-10-18 13:46:00

#### dynamic programming by Bellman

2009年07月17日 3.16MB 下载

#### Dynamic Programming and optimal control

2009年08月01日 7.46MB 下载

#### Dynamic Programming and Optimal Control, Vol 2

2014年06月15日 15.64MB 下载

## 不良信息举报

RNA secondary structure(Dynamic Programming)