Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afe706c

Browse files
RNA Secondary structure
1 parent 7688acd commit afe706c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
// RNA Secondary structure problem (RNA folding)
5+
// OPT(i,j) = max(OPT(i,j-1), (for all t)max(1 + OPT(i,t-1) + OPT(t+1,j-1)));
6+
// All characters in the string are from set {A, U, C, G}
7+
// One blank character at s[0] because we use 1 based indexing.
8+
// Running time of this algorithm is O(n^3)
9+
10+
string s = " ACCGGUAGU";
11+
12+
int DP[100][100];
13+
14+
int OPT(int i, int j)
15+
{
16+
if (DP[i][j] != -1)
17+
return DP[i][j];
18+
19+
if (i <= j - 4)
20+
{
21+
int a = OPT(i, j - 1);
22+
int b = INT_MIN;
23+
for (int t = i; t < j; t++)
24+
if ((s[t] == 'A' && s[j] == 'U') || ((s[t] == 'C') && s[j] == 'G'))
25+
b = max(b, 1 + OPT(i, t - 1) + OPT(t + 1, j - 1));
26+
27+
return DP[i][j] = max(a, b);
28+
}
29+
else
30+
return DP[i][j] = 0;
31+
}
32+
33+
int main()
34+
{
35+
memset(DP, -1, sizeof(DP));
36+
printf("%d\n", OPT(1, s.length() - 1));
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /