zoukankan      html  css  js  c++  java
  • 动态规划求解最长公共子序列

    #include<iostream>
    #include<algorithm>
    using namespace std;
    void Print_LCS(int **b, string X,int m, int n);
    void LCS_Length(string X, string Y)
    {
        int m = X.length();
        int n = Y.length();
        X = "0" + X;
        Y = "0" + Y;
        int **b = new int*[m + 1];
        for (int i = 0; i < m + 1; i++)
        {
            b[i] = new int[n + 1];
        }
        int **c = new int*[m + 1];
        for (int i = 0; i < m + 1;i++)
        {
            c[i] = new int[n + 1];
        }
        for (int i = 1; i < m + 1; i++)
            c[i][0] = 0;
        for (int j = 0; j < n + 1; j++)
            c[0][j] = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (X.at(i) == Y.at(j))
                {
                    c[i][j] = c[i - 1][j - 1] + 1;
                    b[i][j] = 1;
                }    
                else if (c[i - 1][j]>=c[i][j - 1])
                {
                    c[i][j] = c[i - 1][j];
                    b[i][j] = 0;
                }
                else
                {
                    c[i][j] = c[i][j - 1];
                    b[i][j] = -1;
                }
            }
        }
        Print_LCS(b,X,m,n);
        cout << "
    ";
    }
    void Print_LCS(int **b, string X,int m, int n)
    {
        if (m == 0 || n == 0)
            return;
        if (b[m][n] == 1)
        {
            Print_LCS(b,X,m-1,n-1);
            cout << X.at(m)<< "  ";
        }
        else if (b[m][n] == 0)
        {
            Print_LCS(b,X, m - 1, n);
        }
        else
        {
            Print_LCS(b, X,m , n-1);
        }
    }
    //带有备忘的LCS_LENGTH
    int LoopUp_LCS(string X, string Y, int **C,int m, int n)
    {
        if (C[m][n] > -1)
            return C[m][n];
        if (m == 0 || n == 0)
            C[m][n] = 0;
        else
        {
            if (X.at(m - 1) == Y.at(n - 1))
                C[m][n] = LoopUp_LCS(X,Y,C,m-1,n-1) + 1;
            else
            {
                C[m][n] = max(LoopUp_LCS(X, Y, C, m - 1, n), LoopUp_LCS(X, Y, C, m, n-1));
            }
        }
        return C[m][n];
    }
    int**Memoized_LCS_Length(string X, string Y)
    {
        int m = X.length();
        int n = Y.length();
        X = "0" + X;
        Y = "0" + Y;
        int **C = new int*[m + 1];
        for (int i = 0; i < m + 1; i++)
        {
            C[i] = new int[n + 1];
        }
        for (int i = 0; i < m + 1; i++)
        {
            for (int j = 0; j < n + 1; j++)
            {
                C[i][j] = -1;
            }
        }
    
        LoopUp_LCS(X,Y,C,m,n);
        return C;
    }
    //KMP
    
    int *Compute2_Prefix_Function(string P,int m)
    {
        int *t = new int[m + 1];
        t[1] = 0;
        int k = 0;
        for (int q = 2; q <= m; q++)
        {
            while (k > 0 && P[k + 1] != P[q])
                k = t[k];
            if (P[k + 1] == P[q])
                k = k + 1;
            t[q] = k;
        }
        for (int i = 1; i <= m; i++)
        {
            cout << t[i] << " ";
        }
        cout << "
    ";
        return t;
    }
    void KMP2_Matcher(string T, string P)
    {
        int n = T.length();
        int m = P.length();
        T = "0" + T;
        P = "0" + P;
        int *t = Compute2_Prefix_Function(P, m);
        int q = 0;
        for (int i = 1; i <= n; i++)
        {
            while (q > 0 && P[q + 1] != T[i])
                q = t[q];
            if (P[q + 1] == T[i])
                q = q + 1;
            if (q == m)
            {
                cout << "macther at " << i - m;
                q = t[q];
            }
        }
        cout << "
    ";
    
    }
    
    
    int main()
    {
        string X = "ABCBDAB";
        string Y = "BDCABA";
        LCS_Length(X,Y);
        int **C = Memoized_LCS_Length(X, Y);
        for (unsigned  i = 0; i < X.length() + 1; i++)
        {
            for (unsigned j = 0; j < Y.length(); j++)
            {
                cout << C[i][j] << "	";
            }
            cout << "
    ";
        }
        cout << "
    ";
        X = "ababababbc";
        Y = "ababbc";
        KMP2_Matcher(X,Y);
        return 0;
    }
  • 相关阅读:
    Construct Binary Tree from Preorder and Inorder Traversal
    Construct Binary Tree from Inorder and Postorder Traversal
    Maximum Depth of Binary Tree
    Sharepoint 2013 创建TimeJob 自动发送邮件
    IE8 不能够在Sharepoint平台上在线打开Office文档解决方案
    TFS安装与管理
    局域网通过IP查看对方计算机名,通过计算机名查看对方IP以及查看在线所有电脑IP
    JS 隐藏Sharepoint中List Item View页面的某一个字段
    SharePoint Calculated Column Formulas & Functions
    JS 两个一组数组转二维数组
  • 原文地址:https://www.cnblogs.com/liuhg/p/LCS.html
Copyright © 2011-2022 走看看