LCS最长公共子序列板子

string SearchMaxLengthSequence(string s1, string s2)
{
    int length1 = s1.length();
    int length2 = s2.length();
    int** matrix = new int*[length1 + 1];
    for (size_t i = 0; i < length1 + 1; i++)
    {
        matrix[i] = new int[length2 + 1]();
    }

    for (size_t i = 1; i <= length1; i++)
    {
        for (size_t j = 1; j <= length2; j++)
        {
            if (s1[i - 1] == s2[j - 1])
            {
                matrix[i][j] = matrix[i - 1][j - 1] + 1;
            }
            else
            {
                matrix[i][j] = max(matrix[i][j - 1], matrix[i - 1][j]);
            }
        }
    }

    int commomLength = matrix[length1][length2];//最大子序列的长度
    string commomStr = "";//保存逆序的子序列
    //回溯部分
    while (length1 != 0)
    {
        if (matrix[length1][length2] == matrix[length1 - 1][length2])
        {
            length1--;
            continue;
        }
        if (matrix[length1][length2] == matrix[length1][length2 - 1])
        {
            length2--;
            continue;
        }
        if (matrix[length1][length2] == matrix[length1 - 1][length2 - 1] + 1)
        {
            commomStr += s1[length1 - 1];
            length1--;
            length2--;
        }
    }
    reverse(commomStr.begin(), commomStr.end());
    return commomStr;

}

猜你喜欢

转载自blog.csdn.net/qq_42825221/article/details/82813311
今日推荐