简体   繁体   English

找到最长的回文 DNA 子序列,上面有最多的突变

[英]Find the longest palindromic DNA sub-sequence that has the most mutations on it

I've been trying to do a Dynamic Programming assignment for university but I had no success so far.我一直在尝试为大学做动态编程作业,但到目前为止我没有成功。

The problem:问题:

Given a DNA string and a list of mutation locations (for exemple, pieces 0 and 2 are mutations), find the longest palindromic sub-sequence that contains the most mutations on it.给定一个 DNA 字符串和一个突变位置列表(例如,片段 0 和 2 是突变),找到包含最多突变的最长回文子序列。

Input: a string S with 0 to 2000 chars;输入:一个 0 到 2000 个字符的字符串 S; an integer N such that 0<=N<=|S|一个整数 N 使得 0<=N<=|S| and N positions (numbers from 0 to |S|) of mutations.和 N 个位置(从 0 到 |S| 的数字)突变。

Output: an integer representing the size of the longest palindromic sub-sequence containing the maximum number of mutations.输出:一个整数,表示包含最大突变数的最长回文子序列的大小。

Examples:例子:

Input: CAGACAT 0输入:CAGACAT 0

Output: 5输出:5

Input: GATTACA 1 0输入:GATTACA 1 0

Output: 1输出:1

Input: GATTACA 3 0 4 5输入:GATTACA 3 0 4 5

Output: 3输出:3

Input: TATACTATA 2 4 8输入:TATACTATA 2 4 8

Output: 7输出:7

We have to code it in C, but what I really need are ideas, any language or pseudo-code is good to me.我们必须用 C 编写它,但我真正需要的是想法,任何语言或伪代码对我来说都很好。

My code to find the LPS (in C)我查找 LPS 的代码(在 C 中)

int find_lps(char *input)
{
    int len = strlen(input), i, cur_len;
    int c[len][len];

    for (i = 0; i < len; i++)
        c[i][i] = 1;

    for (cur_len = 1; cur_len < len; cur_len++) {
        for (i = 0; i < len - cur_len; i++) {
            int j = i + cur_len;

            if (input[i] == input[j]) {
                c[i][j] = c[i + 1][j - 1] + 2;
            } else {
                c[i][j] = max(c[i + 1][j], c[i][j - 1]);
            }
        }
    }

    return c[0][len - 1];
}

What I tried to do for the mutations:我试图为突变做些什么:

1- Creating an array of places where the LPS is changed. 1- 创建 LPS 更改位置的数组。 That doesn't work, and really, I have no idea of what to do.那行不通,真的,我不知道该怎么做。

More details about the problem: In a situation where you have n palindromic subsequences, both of them with the same size of mutations inside, I need the longest of them.有关问题的更多详细信息:在您有 n 个回文子序列的情况下,它们内部的突变大小相同,我需要它们中最长的一个。 Given that you have n palindromic subsequences with X mutations, (we have M mutations), I need the longest palindromic subsequence of X mutations, considering you don't have a palindromic subsequence with M mutations.鉴于您有 n 个带有 X 突变的回文子序列(我们有 M 突变),考虑到您没有带有 M 突变的回文子序列,我需要最长的 X 突变回文子序列。 If you do, then you should choose the other subsequence, even if it's shorter.如果你这样做,那么你应该选择另一个子序列,即使它更短。 So, first criteria: most mutations in a palindromic subsequence.所以,第一个标准:回文子序列中的大多数突变。 If we have the same amount, then the longest of the subsequences.如果我们有相同的数量,那么最长的子序列。

Any help is appreciated, thank you.任何帮助表示赞赏,谢谢。

Lets define C[i][j] to store 2 values:让我们定义 C[i][j] 来存储 2 个值:

1- The length of the longest palindromic sub-sequence in the sub-string S(i,j) that contains the most mutations in it, and lets denote it by C[i][j].len 1- 子串 S(i,j) 中包含最多突变的最长回文子序列的长度,用C[i][j].len 表示

2- The number of mutations in the longest palindromic sub-sequence in the sub-string S(i,j) that contains the most mutations in it, and lets denote it by C[i][j].ms 2- 子串 S(i,j) 中包含最多突变的最长回文子序列的突变数,用C[i][j].ms 表示

Then the result of the problem would be C[0][|S|-1].len那么问题的结果将是 C[0][|S|-1].len

Note : m[i] = 1 means the character s[i] is a mutation, otherwise m[i] = 0注意:m[i] = 1 表示字符 s[i] 是一个变异,否则 m[i] = 0

Here is the full code written in c++:这是用 C++ 编写的完整代码:

#include <iostream>
#include <string>
using namespace std;


string s;
int m[2001];

struct Node {
    int ms;//number of mutations
    int len;

    Node() {
        ms = len = 0;
    }

    Node(int v1,int v2) {
        ms = v1;
        len = v2;
    }
};

Node C[2001][2001];


Node getBestNode(Node n1, Node n2) {
    if (n1.ms > n2.ms)
        return n1;

    if (n1.ms <  n2.ms)
        return n2;

    if (n1.len > n2.len)
        return n1;

    if (n1.len < n2.len)
        return n2;  

    return n1;
}

void init() {
    for (int i = 0; i < 2001; i++) {
        m[i] = 0;
        for (int j = 0; j < 2001; j++)  C[i][j] = Node(0,0);
    }
}

void solve() {
    int len = s.length();

    // initializing the ranges of length = 1
    for (int i = 0; i < len; i++)
        C[i][i] = Node( m[i],1 );

    // initializing the ranges of length = 2
    for (int i = 0; i < len - 1; i++) 
        if (s[i] == s[i + 1])
            C[i][i + 1] = Node(m[i] + m[i + 1],2);
        else if (m[i] || m[i + 1])
                C[i][i + 1] = Node(1,1) ;

    // for ranges of length >= 3
    for (int cur_len = 3; cur_len <= len; cur_len++)
        for (int i = 0; i <= len - cur_len; i++) {

            int j = i + cur_len - 1;

            C[i][j] = getBestNode(C[i + 1][j], C[i][j-1]);

            if (s[i] == s[j]) {
                Node nn = Node(
                    C[i + 1][j - 1].ms + m[i] + m[j] , 
                    C[i + 1][j - 1].len + 2
                );

                C[i][j] = getBestNode(C[i][j], nn);
            }
        }
}

int main() {

    int n;  
    cin >> s >> n;

    init();//initializing the arrays with zeros

    for (int i = 0; i < n; i++) {
        int x;  cin >> x;
        m[x] = 1;
    }

    solve();

    cout << C[0][s.length()-1].len << endl; 
    return 0;
}

The function getBestNode() is returning the best of 2 solutions by considering the number of mutations then the length of the sub-sequence.函数getBestNode()通过考虑突变数量和子序列的长度来返回 2 个解决方案中的最佳解决方案。

Note : The code can be shorter, but I made it this way for clarity.注意:代码可以更短,但为了清楚起见,我这样做了。

尝试访问我的页面https://github.com/keroykeroy/Longest-Palindromic-Sequence,但我使用php查找最长的回文序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM